Python:从ISO-8859-1 / latin1转换为UTF-8 [英] Python: Converting from ISO-8859-1/latin1 to UTF-8
本文介绍了Python:从ISO-8859-1 / latin1转换为UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个字符串已经从电子邮件模块从Quoted-printable到ISO-8859-1解码。这给了我像\xC4pple这样的字符串,它对应于Äpple(苹果在瑞典语中)。
然而,我不能将这些字符串转换为UTF-8。
> apple =\xC4pple
>>>> apple
'\xc4pple'
>>>> apple.encode(UTF-8)
回溯(最近一次调用):
在< module>中的文件< stdin&
UnicodeDecodeError:'ascii'编解码器无法解码位置0中的字节0xc4:顺序不在范围内(128)
b $ b
我该怎么办?
解决方案
尝试先解码,然后编码:
apple.decode('iso-8859-1')。encode('utf8')
pre>
I have this string that has been decoded from Quoted-printable to ISO-8859-1 with the email module. This gives me strings like "\xC4pple" which would correspond to "Äpple" (Apple in Swedish). However, I can't convert those strings to UTF-8.
>>> apple = "\xC4pple" >>> apple '\xc4pple' >>> apple.encode("UTF-8") Traceback (most recent call last): File "<stdin>", line 1, in <module> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc4 in position 0: ordinal not in range(128)
What should I do?
解决方案Try decoding it first, then encoding:
apple.decode('iso-8859-1').encode('utf8')
这篇关于Python:从ISO-8859-1 / latin1转换为UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文