【问题标题】:UnicodeDecodeError: 'ascii' codec can't decodeUnicodeDecodeError:“ascii”编解码器无法解码
【发布时间】:2011-09-26 08:13:33
【问题描述】:

我正在使用 file.readline() 读取包含 Python 中罗马尼亚语单词的文件。 由于编码,我遇到了很多字符的问题。

例子:

>>> a = "aberație"  #type 'str'
>>> a -> 'abera\xc8\x9bie'
>>> print sys.stdin.encoding
UTF-8

我已经尝试使用 utf-8、cp500 等进行 encode(),但它不起作用。

我找不到我必须使用的正确字符编码?

提前致谢。

编辑:目的是将文件中的单词存储在字典中,并在打印时获取 aberație 而不是 'abera\xc8\x9bie'

【问题讨论】:

    标签: python file encoding decoding representation


    【解决方案1】:

    你想做什么?

    这是一组字节:

    BYTES = 'abera\xc8\x9bie'
    

    这是一组字节,表示字符串“aberaşie”的utf-8 编码。您解码字节以获取您的 unicode 字符串:

    >>> BYTES 
    'abera\xc8\x9bie'
    >>> print BYTES 
    aberație
    >>> abberation = BYTES.decode('utf-8')
    >>> abberation 
    u'abera\u021bie'
    >>> print abberation 
    aberație
    

    如果要将 unicode 字符串存储到文件中,则必须将其编码为您选择的特定字节格式:

    >>> abberation.encode('utf-8')
    'abera\xc8\x9bie'
    >>> abberation.encode('utf-16')
    '\xff\xfea\x00b\x00e\x00r\x00a\x00\x1b\x02i\x00e\x00'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-06
      • 1970-01-01
      • 1970-01-01
      • 2013-08-20
      • 2014-04-09
      • 2018-08-02
      • 2013-09-23
      • 2013-06-17
      相关资源
      最近更新 更多