Reputation: 3854
I was trying to parse twitter data. I retrieved the data and stored it in a file named 'twitterdata'
f = open('twitterdata','r')
for line in f:
jsonline = json.loads(line)
for key in jsonline:
print str(jsonline[key]).encode('utf-8')
I am getting the error even 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)
Upvotes: 1
Views: 2878
Reputation: 36668
Drop the str()
, or change it to unicode()
:
print jsonline[key].encode('utf-8')
or
print unicode(jsonline[key]).encode('utf-8')
In Python 2.x, str()
tries to convert its contents to 8-bit strings. Since you're passing it Unicode objects, it uses the default encoding (ascii
) and fails, before the .encode('utf-8')
call is reached. Using unicode()
is redundant here if the data you're getting is all text, but will be useful if some of it is, say, integers, so I recommend the latter.
Upvotes: 4