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
Post a Comment