加入时的UnicodeDecodeError

时间:2011-12-12 21:18:58

标签: python unicode character-encoding

我有一个包含一些字符串的列表(大多数是从sqlite3数据库中获取的):

stats_list = ['Statistik \xc3\xb6ver s\xc3\xa5nger\n', 'Antal\tS\xc3\xa5ng', '1\tCarola - Betlehems Stj\xc3\xa4rna', '\n\nStatistik \xc3\xb6ver datak\xc3\xa4llor\n', 'K\xc3\xa4lla\tAntal', 'MANUAL\t1', '\n\nStatistik \xc3\xb6ver \xc3\xb6nskare\n', 'Antal\tId', u'1\tNiclas']

当我尝试加入时:

return '\n'.join(stats_list)

我收到此错误:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 10: ordinal not in range(128)

是否有可能通过查看列表得到任何线索?如果我遍历列表并将其打印到屏幕上,我会得到这个:

Statistik över sånger

Antal   Sång 
1   Carola - Betlehems Stjärna


Statistik över datakällor

Källa   Antal 
MANUAL  1


Statistik över önskare

Antal   Id
1   Niclas

这正是我所期待的,并且没有显示错误。 (特殊字符是瑞典语。)

编辑:

我会试试这个:

   return '\n'.join(i.decode('utf8') for i in stats_list)

但它回来了:

Traceback (most recent call last):
  File "./CyberJukebox.py", line 489, in on_stats_to_clipboard
    stats = self.jbox.get_stats()
  File "/home/nine/dev/python/CyberJukebox/jukebox.py", line 235, in get_stats
    return self._stats.get_string()
  File "/home/nine/dev/python/CyberJukebox/jukebox.py", line 59, in get_string
    return '\n'.join(i.decode('utf8') for i in stats_list)
  File "/home/nine/dev/python/CyberJukebox/jukebox.py", line 59, in <genexpr>
    return '\n'.join(i.decode('utf8') for i in stats_list)
  File "/usr/lib/python2.7/encodings/utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in position 10: ordinal not in range(128)

编辑2:

建议的解决方案适用于口译员。但是当我执行代码时,它将无法工作。我无法绕过这个。也许这是显而易见的我错过了所以我在这里粘贴整个方法:

 def get_string(self):
     stats_list = [u'Statistik över sånger\n', u'Antal\tSång']
     stats = sorted([(v, k) for k, v in self.song_stats.iteritems()], reverse=True)
     for row in stats:
         line = '%s\t%s' % row
         stats_list.append(line)

     stats_list.append(u'\n\nStatistik över datakällor\n')
     stats_list.append(u'Källa\tAntal')
     stats = sorted([(k, v) for k, v in self.exts_stats.iteritems()])
     for row in stats:
         line = '%s\t%s' % row
         stats_list.append(line)

     stats_list.append(u'\n\nStatistik över önskare\n')
     stats_list.append(u'Antal\tId')
     stats = sorted([(v, k) for k, v in self.wisher_stats.iteritems() if k != ''], reverse=True)
     for row in stats:
         line = '%s\t%s' % row
         stats_list.append(line)

     return '\n'.join(i.decode('utf8') for i in stats_list)

song_statsexts_statswisher_stats是班上的词典。

3 个答案:

答案 0 :(得分:11)

您的问题可能是您正在使用字节字符串混合unicode字符串。

&#34;编辑2&#34;中的代码将几个 unicode 字符串添加到stats_list

stats_list = [u'Statistik över sånger\n', u'Antal\tSång']

如果您尝试解码这些unicode字符串,您将获得UnicodeEncodeError。这是因为在尝试解码之前,Python会首先尝试使用默认编码(通常是&#34; ascii&#34;)来编码字符串。只解码 byte 字符串才有意义。

首先,将函数中的最后一行更改为:

return '\n'.join(stats_list)

现在您需要检查添加到stats_list的任何其他字符串是否为 byte 字符串,并确保它们正确解码为 unicode 字符串第一

所以把print type(line)放在这三行之后:

line = '%s\t%s' % row

然后在打印<type 'str'>的任何地方,将跟随行更改为:

stats_list.append(line.decode('utf-8'))

当然,如果它打印<type 'unicode'>,则无需更改以下行。

这里更好的解决方案是检查字典song_statsexts_statswisher_stats的创建方式,并确保它们始终包含unicode字符串(或仅包含字节字符串) ascii characters)。

答案 1 :(得分:7)

字符串以UTF-8编码。您需要.decode他们到unicode

>>> 'Statistik \xc3\xb6ver s\xc3\xa5nger\n'.decode('utf-8')
u'Statistik \xf6ver s\xe5nger\n'
>>> print _
Statistik över sånger

使用理解对所有元素执行此操作:

return '\n'.join(x.decode('utf-8') for x in stats_list)

答案 2 :(得分:1)

Python抱怨它无法将字符串'Statistik \xc3\xb6ver s\xc3\xa5nger\n'转换为ASCII字符串。尝试使用u为所有UNICODE字符串添加前缀。

stats_list = [u'Statistik \xc3\xb6ver s\xc3\xa5nger\n', u'Antal\tS\xc3\xa5ng', u'1\tCarola - Betlehems Stj\xc3\xa4rna', u'\n\nStatistik \xc3\xb6ver datak\xc3\xa4llor\n', u'K\xc3\xa4lla\tAntal', u'MANUAL\t1', u'\n\nStatistik \xc3\xb6ver \xc3\xb6nskare\n', u'Antal\tId', u'1\tNiclas']