我正在通过一个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,这样它就会返回:“六月有三十天”
当我试图解码或编码这个unicode字符串时,它会抛出一个错误:
^{pr2}$如果我将字符串初始化为纯文本,然后对其进行解码,则可以:
>>>title = 'There\x92s thirty days in June'
>>> type(title)
<type 'str'>
>>>print title.decode('cp1252')
There’s thirty days in June
我的问题是如何将得到的unicode字符串转换成纯文本字符串以便解码?在
似乎您的字符串是用
latin1
(因为它是unicode
类型)解码的latin1
)对其进行编码unicode
),你必须使用正确的编解码器(cp1252
)解码utf-8
字节,你必须使用UTF-8
编解码器对进行编码。在代码:
取决于API是接受文本(
unicode
)还是bytes
,3。可能没有必要。在相关问题 更多 >
编程相关推荐