如何将字符串从CP-1251转换为UTF-8? [英] How to convert a string from CP-1251 to UTF-8?
问题描述
我正在使用诱变剂从 CP-1251 /<转换ID3标签数据a href ="https://en.wikipedia.org/wiki/Windows-1252" rel ="noreferrer"> CP-1252 转换为UTF-8.在Linux中没有问题.但是在Windows上,在wx.TextCtrl上调用 SetValue()
产生错误:
I'm using mutagen to convert ID3 tags data from CP-1251/CP-1252 to UTF-8. In Linux there is no problem. But on Windows, calling SetValue()
on a wx.TextCtrl produces the error:
UnicodeDecodeError:"ascii"编解码器无法解码位置中的字节0xc3 0:序数不在范围内(128)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in range(128)
我从诱变中提取的原始字符串(假定为CP-1251编码)是:
The original string (assumed to be CP-1251 encoded) that I'm pulling from mutagen is:
u'\xc1\xe5\xeb\xe0\xff \xff\xe1\xeb\xfb\xed\xff \xe3\xf0\xee\xec\xf3'
我尝试将其转换为UTF-8:
I've tried converting this to UTF-8:
dd = d.decode('utf-8')
...甚至将默认编码从ASCII更改为UTF-8:
...and even changing the default encoding from ASCII to UTF-8:
sys.setdefaultencoding('utf-8')
...但是我遇到了同样的错误.
...But I get the same error.
推荐答案
如果您确定输入中包含cp1251,则可以这样做
If you know for sure that you have cp1251 in your input, you can do
d.decode('cp1251').encode('utf8')
这篇关于如何将字符串从CP-1251转换为UTF-8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!