无法使用 python 解码这个字符串 [英] unable to decode this string using python
问题描述
我有这个 text.ucs 文件,我正在尝试使用 python 对其进行解码.
I have this text.ucs file which I am trying to decode using python.
file = open('text.ucs', 'r')
content = file.read()
print content
我的结果是
\xf\xe\x002\22
\xf\xe\x002\22
我尝试使用 utf-16、utf-8 进行解码
I tried doing decoding with utf-16, utf-8
content.decode('utf-16')
出现错误
回溯(最近一次调用最后一次):文件",第 1 行,在文件C:\Python27\lib\encodings\utf_16.py",第 16 行,在解码返回 codecs.utf_16_decode(input, errors, True) UnicodeDecodeError: 'utf16' codec can't decode bytes in position32-33:非法编码
Traceback (most recent call last): File "", line 1, in File "C:\Python27\lib\encodings\utf_16.py", line 16, in decode return codecs.utf_16_decode(input, errors, True) UnicodeDecodeError: 'utf16' codec can't decode bytes in position 32-33: illegal encoding
如果我遗漏了什么或我的方法有误,请告诉我
Please let me know if I am missing anything or my approach is wrong
已要求截图
推荐答案
oooh,据我所知,您使用的是 python 2.xx,但据我所知,仅在 python 3.xx 中添加了编码参数,我不精通python 2.xx 但你可以在谷歌搜索 io.open 例如尝试:
oooh, as i understand you using python 2.x.x but encoding parameter was added only in python 3.x.x as I know, i am doesn't master of python 2.x.x but you can search in google about io.open for example try:
file = io.open('text.usc', 'r',encoding='utf-8')
content = file.read()
print content
但是请检查您是否需要导入 io 模块
but chek do you need import io module or not
这篇关于无法使用 python 解码这个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!