Python:UnicodeEncodeError:'latin-1'编解码器不能编码字符

时间:2011-11-28 00:12:05

标签: python unicode encode

我正处于一个我调用api的场景,并根据api的结果我为api中的每条记录调用数据库。我的api调用返回字符串,当我通过api为数据库调用返回的项时,对于某些元素,我得到以下错误。

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')背后的理论是什么,如果我能对上面尝试的内容得到一些解释,那将会很棒。

3 个答案:

答案 0 :(得分:14)

如果您需要Latin-1编码,您有几个选项可以摆脱en-dash或255以上的其他代码点(Latin-1中未包含的字符):

>>> u = u'hello\u2013world'
>>> u.encode('latin-1', 'replace')    # replace it with a question mark
'hello?world'
>>> u.encode('latin-1', 'ignore')     # ignore it
'helloworld'

或者进行自己的自定义替换:

>>> u.replace(u'\u2013', '-').encode('latin-1')
'hello-world'

如果您不需要输出Latin-1,那么UTF-8是一种常见且首选的选择。它是由W3C推荐的,可以很好地编码所有Unicode代码点:

>>> u.encode('utf-8')
'hello\xe2\x80\x93world'

答案 1 :(得分:2)

unicode字符u'\ 02013'是“en dash”。它包含在Windows-1252(cp1252)字符集(编码为x96)中,但不包含在Latin-1(iso-8859-1)字符集中。 Windows-1252字符集在x80 - x9f中定义了更多字符,其中包括en dash。

解决方案是让您选择与Latin-1不同的目标字符集,例如Windows-1252或UTF-8,或者用简单的“ - ”替换en dash。

答案 2 :(得分:1)

u.encode('utf-8')将其转换为字节,然后可以使用sys.stdout.buffer.write(bytes)在stdout上打印 结帐显示挂钩 https://docs.python.org/3/library/sys.html