Tôi đang ở một tình huống mà tôi gọi api và dựa trên kết quả từ api tôi gọi cơ sở dữ liệu cho mỗi bản ghi mà tôi trong api. Chuỗi trả về cuộc gọi api của tôi và khi tôi thực hiện lệnh gọi cơ sở dữ liệu cho các mục trả về bởi api, đối với một số yếu tố, tôi nhận được lỗi sau.Python: UnicodeEncodeError: 'latin-1' codec không thể mã hóa ký tự
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)
Các phân đoạn của mã của tôi lỗi trên được đề cập là:
...
for startCategory in value[0]:
categoryResults = []
try:
categoryRow = ""
baseCategoryTree[startCategory] = []
#print categoryQuery % {'title': startCategory};
cursor.execute(categoryQuery, {'title': startCategory}) #unicode issue
done = False
cont...
Sau khi thực hiện một số tìm kiếm google tôi đã cố gắng sau đây trên dòng lệnh của tôi để hiểu whats going on ...
>>> 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'
Nhưng tôi không chắc chắn sẽ là giải pháp để khắc phục vấn đề này. Ngoài ra tôi không biết lý thuyết đằng sau encode('cp1252')
nó sẽ là tuyệt vời nếu tôi có thể nhận được một số lời giải thích về những gì tôi đã thử ở trên.
trùng lặp có thể xảy ra của [UnicodeEncodeError : 'latin-1' codec không thể mã hóa ký tự] (http://stackoverflow.com/questions/3942888/unicodeencodeerror-latin-1-codec-cant-encode-character) –