定义
下面是打开文件'problem_1_submission.txt'的代码。文件'problem_1_submission.txt'是20行推特消息的列表。
这些Twitter消息然后json.load并编译到列表'tweet'。
之后,此代码旨在打印出twitter消息的文本。
'''
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']
问题
此代码无法打印出twitter消息的文本。出现的错误是UnicodeEncodeError。
这是错误:
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
有什么想法吗?