【问题标题】:encoding string to utf in python在python中将字符串编码为utf
【发布时间】:2015-03-30 04:22:29
【问题描述】:

我对 python 中的编码相当困惑。我有以下字符串。

s = "Caf\xe9/Coffee/Tea"

我想让它成为一个 unicode 字符串,以便它能够正确显示。以下作品:

t = u"Caf\xe9/Coffee/Tea"

print t

输出是“Café/Coffee/Tea”

但是如果我尝试

r = unicode(s)

我收到错误“UnicodeDecodeError: 'ascii' codec can't decode byte 0xf1 in position 3: ordinal not in range(128)”

执行此操作时,我什至没有尝试将 unicode 字符串显示到控制台(我最近了解到的称为“heisenbug”)。但似乎我的控制台可以打印 unicode,所以我真的不明白这个问题。

如果重要的话,这是 python 2.7。

【问题讨论】:

  • 该字符串(字节)已被编码。你需要.decode它来获取一个unicode对象。
  • 如果没有什么能阻止你回到 Python 2,我建议切换到 Python 3 - 你对 Unicode 的困惑会少得多。
  • 是的。我想切换到 python 3,但我正在使用 python 的 anaconda 发行版......而且它仍然在 2.7 上。

标签: python unicode encoding utf-8


【解决方案1】:

使用

>>> "Caf\xe9/Coffee/Tea".decode('iso-8859-1')

虽然我建议尽可能在任何地方使用 UTF-8 进行 Unicode 编码。

【讨论】:

  • 哼哼,我不知道你用的是什么字符集,不是latin-1; \xf1 在拉丁语 1 中是 ñ
  • 我不认为 'iso-8859-1' 是正确的编码。这会给你 ñ 而不是 é。
  • é 是 \xe9,但ñ 是 \xf1
  • @Antti。我在粘贴时出错了。应该是“Caf\xe9/Coffee/Tea”。对此感到抱歉。
  • 是的,@Aaron,\xf1 在任何代码页中都不会出现 é,只是经过测试。
【解决方案2】:

使用 "Latin-1' 将该字符串解码为 Unicode。

>>> s = "Caf\xe9/Coffee/Tea"
>>> r = unicode(s, 'Latin-1')
>>> print r

Café/Coffee/Tea

【讨论】:

    【解决方案3】:

    声明你的源文件的编码,你可以直接在文件中写入Unicode字符:

    # coding: utf8
    s = 'Café/Coffee/Tea'
    print repr(s)
    print s
    t = u'Café/Coffee/Tea'
    print t
    

    输出(注意我的控制台使用编码cp437):

    'Caf\xc3\xa9/Coffee/Tea'
    Café/Coffee/Tea
    Café/Coffee/Tea
    

    第一行是一个字节串。它将在源编码中。由于 UTF-8 被发送到代码页 437 终端,第二行无法正确打印。 Python 知道将 Unicode 字符串编码为控制台编码,因此第 3 行打印正确。

    这里源被声明并以不同的编码保存。注意打印 Unicode 还是正确的:

    # coding: iso-8859-1
    s = 'Café/Coffee/Tea'
    print repr(s)
    print s
    t = u'Café/Coffee/Tea'
    print t
    

    输出:

    'Caf\xe9/Coffee/Tea'
    CafΘ/Coffee/Tea
    Café/Coffee/Tea
    

    【讨论】:

      【解决方案4】:

      unicode(bytestring) 函数尝试使用 Python 2 上的默认编码 ('ascii') 对输入字节串进行解码。有超出 ascii 范围的字节,因此它失败了。

      注意"\xf1"u"\xf1"在Python 2上非常不同。前者是字节串,后者是Unicode字符串:

      >>> "\xf1".decode('cp866')
      u'\u0451'
      >>> "\xf1".decode('cp437')                                                                                
      u'\xb1'
      >>> "\xf1".decode('latin-1')                                                                              
      u'\xf1'
      >>> u"\xf1".encode('cp866')
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
        File "/usr/lib/python2.7/encodings/cp866.py", line 12, in encode
          return codecs.charmap_encode(input,errors,encoding_map)
      UnicodeEncodeError: 'charmap' codec can't encode character u'\xf1' in position 0: character maps to <undefined>
      >>> u"\xf1".encode('cp437')
      '\xa4'
      >>> u"\xf1".encode('latin-1')
      '\xf1'
      

      There Ain't No Such Thing as Plain Text。如果要将字节转换为文本;你应该指定字符编码。

      【讨论】:

        猜你喜欢
        • 2016-05-16
        • 2011-08-09
        • 2019-11-03
        • 2020-11-29
        • 2016-01-11
        • 2014-01-18
        • 1970-01-01
        • 2011-08-27
        相关资源
        最近更新 更多