我在一個場景,我調用api並基於api的結果我調用數據庫中的每個記錄,我在api中。我的api調用返回字符串,當我使用api返回的數據庫調用時,對於某些元素,我得到以下錯誤。Python:UnicodeEncodeError:'latin-1'編解碼器無法編碼字符
Traceback (most recent call last):
File "TopLevelCategories.py", line 267, in <module>
cursor.execute(categoryQuery, {'title': startCategory});
File "/opt/ts/python/2.7/lib/python2.7/site-packages/MySQLdb/cursors.py", line 158, in execute
query = query % db.literal(args)
File "/opt/ts/python/2.7/lib/python2.7/site-packages/MySQLdb/connections.py", line 265, in literal
return self.escape(o, self.encoders)
File "/opt/ts/python/2.7/lib/python2.7/site-packages/MySQLdb/connections.py", line 203, in unicode_literal
return db.literal(u.encode(unicode_literal.charset))
UnicodeEncodeError: 'latin-1' codec can't encode character u'\u2013' in position 3: ordinal not in range(256)
我的代碼上面的錯誤所指的路段是:
...
for startCategory in value[0]:
categoryResults = []
try:
categoryRow = ""
baseCategoryTree[startCategory] = []
#print categoryQuery % {'title': startCategory};
cursor.execute(categoryQuery, {'title': startCategory}) #unicode issue
done = False
cont...
做了一些谷歌搜索後,我嘗試了我的命令行下面來了解怎麼回事...
>>> import sys
>>> u'\u2013'.encode('iso-8859-1')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'latin-1' codec can't encode character u'\u2013' in position 0: ordinal not in range(256)
>>> u'\u2013'.encode('cp1252')
'\x96'
>>> '\u2013'.encode('cp1252')
'\\u2013'
>>> u'\u2013'.encode('cp1252')
'\x96'
但我不確定解決方案將如何解決此問題。我也不知道encode('cp1252')
背後的理論是什麼,如果我可以對我上面的嘗試做一些解釋,那將是非常好的。
的可能的複製[UnicodeEncodeError :'拉丁-1'編解碼器不能編碼字符](http://stackoverflow.com/questions/3942888/unicodeencodeerror-latin-1-codec-cant-encode-character) –