Definition
Below is a code that open a file 'problem_1_submission.txt'. file 'problem_1_submission.txt' is a list of 20 lines of twitter messages.
These twitter messages then json.load and compiled unto list 'tweet'.
After that this code is intended to print out the text of twitter messages.
'''
testing open and store all lines on problem_1_submission.txt to dictionary
'''
import json
import sys
tweet = []
TweetFile = open("problem_1_submission.txt")
for line in TweetFile:
tweet.append(json.loads(line))
'''tweet is a list of dict file. each dict file is a product of json,loads of
each line in problem_1_submission.txt.
the problem_1_submission.txt is basically text file that list 20 lines of twitter
message'''
print 'Tweetrange:',range(len(tweet))
print 'len(tweet):', len(tweet)
for index in range(len(tweet)):
if 'text' in tweet[index]: #Check for tweets with 'text' only!
print 'Tweet index:',index,'tweettext:',tweet[index]['text']
Problem
This code failed to print out the text of twitter messages. The error appeared is UnicodeEncodeError.
this is the error:
PS C:\users\diancharlo\desktop> python TestJsonLoadsStackOverflow.py
Tweetrange: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
len(tweet): 20
Tweet index: 1 tweettext: Traceback (most recent call last):
File "TestJsonLoadsStackOverflow.py", line 23, in <module>
print 'Tweet index:',index,'tweettext:',tweet[index]['text']
File "C:\Python27\lib\encodings\cp437.py", line 12, in encode
return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-12: character
maps to <undefined>
PS C:\users\diancharlo\desktop> py
Any idea?