python - 如何使用 Python 将具有 cp1252 字符的 unicode 字符串转换为 UTF-8?

标签 python unicode encoding utf-8 cp1252

我通过 API 获取文本,该 API 返回带有 Windows 编码撇号 (\x92) 的字符:

> python
>>> title = u'There\x92s thirty days in June'
>>> title
u'There\x92s thirty days in June'
>>> print title
Theres thirty days in June
>>> type(title)
<type 'unicode'>

我正在尝试将此字符串转换为 UTF-8,以便它返回:“There's thyday days in June”

当我尝试解码或编码这个 unicode 字符串时,它抛出一个错误:

>>> title.decode('cp1252')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/cp1252.py", line 15, in decode
    return codecs.charmap_decode(input,errors,decoding_table)
UnicodeEncodeError: 'ascii' codec can't encode character u'\x92' in position 5: ordinal not in range(128)

>>> title.encode("cp1252").decode("utf-8")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/cp1252.py", line 12, in encode
    return codecs.charmap_encode(input,errors,encoding_table)
UnicodeEncodeError: 'charmap' codec can't encode character u'\x92' in position 5: character maps to <undefined>

如果我将字符串初始化为纯文本然后对其进行解码,它会起作用:

>>>title = 'There\x92s thirty days in June'
>>> type(title)
<type 'str'>
>>>print title.decode('cp1252')
There’s thirty days in June

我的问题是如何将我正在获取的 unicode 字符串转换为纯文本字符串以便我可以对其进行解码?

最佳答案

你的字符串似乎是用latin1解码的(因为它是unicode类型)

  1. 要将其转换回原来的字节,您需要使用该编码 (latin1) 编码
  2. 然后要返回文本 (unicode),您必须使用正确的编解码器 (cp1252)解码
  3. 最后,如果您想获得 utf-8 字节,您必须使用 UTF-8 编解码器编码

在代码中:

>>> title = u'There\x92s thirty days in June'
>>> title.encode('latin1')
'There\x92s thirty days in June'
>>> title.encode('latin1').decode('cp1252')
u'There\u2019s thirty days in June'
>>> print(title.encode('latin1').decode('cp1252'))
There’s thirty days in June
>>> title.encode('latin1').decode('cp1252').encode('UTF-8')
'There\xe2\x80\x99s thirty days in June'
>>> print(title.encode('latin1').decode('cp1252').encode('UTF-8'))
There’s thirty days in June

根据 API 是采用文本 (unicode) 还是 bytes,3. 可能不是必需的。

关于python - 如何使用 Python 将具有 cp1252 字符的 unicode 字符串转换为 UTF-8?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45292526/

相关文章:

python - 如何仅返回特定文件类型的文件?

python - 使用 for 循环的二进制搜索,在列表中搜索单词并进行比较

ios - 将Unicode转义序列转换为相应的字符

unicode - 终端 Emacs 中的删除线

python-3.x - python3的编码问题并单击包

java - 转换后的 Word 文档(从 Windows-1252 到 UTF-8)无法正确显示字符

php - 帮助创建 ZIP 文件 Windows 不会阻止

python - 使用命令行垂直分割csv文件

python dataframe 根据另一列创建一列

unicode - 在 PyPlot.jl 中,如何让 unicode 字符正确显示?