我正在研究一个代码,它根据搜索词从Twitter获取实时tweets并将其保存到Mysql数据库。但是,当我运行的代码,而插入到数据库它提出了一个错误:UnicodeDecodeError:'ascii'编解码器无法解码位置139中的字节0xe2:序号不在范围内(128)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 139: ordinal not in range(128)
我不明白有什么问题就在这里是代码插入到数据库
tweet = json.loads(data);
#print json.dumps(tweet, indent=4, sort_keys=True)
#print tweet['text']
tweetid = tweet['id_str']
userid = tweet['user']['id_str']
text = tweet['text'].encode('utf-8')
cur.execute("""INSERT INTO twitterfeeeds(tweet_id, user_id,body,status) VALUES (%s,%s,%s,'0')"""%(tweetid,userid,text))
db.commit()
这里的身体是在鸣叫文本状态是它是否被处理。
鸣叫时不进行编码,它提出了另一个错误 UnicodeEncodeError:“拉丁-1 '编解码器不能在位置234对字符u'\ u2026'进行编码:序号不在范围内(256) – Harwee
@Harwee:不作为查询参数传递时。 –
@Harwee:您确实需要将数据库配置为接受UTF-8 Unicode文本,目前您的数据库只能处理Latin-1。 –