twitter - Encode error in python -


i trying parse twitter data. retrieved data , stored in file named 'twitterdata'

f = open('twitterdata','r') line in f:     jsonline = json.loads(line)     key in jsonline:         print str(jsonline[key]).encode('utf-8') 

i getting error after using encode('utf-8'):

print str(jsonline[key]).encode('utf-8') unicodeencodeerror: 'ascii' codec can't encode characters in position 0-17: ordinal not in     range(128) 

drop str(), or change unicode():

print jsonline[key].encode('utf-8') 

or

print unicode(jsonline[key]).encode('utf-8') 

in python 2.x, str() tries convert contents 8-bit strings. since you're passing unicode objects, uses default encoding (ascii) , fails, before .encode('utf-8') call reached. using unicode() redundant here if data you're getting text, useful if of is, say, integers, recommend latter.


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -