[英]unable to decode this string using python
我有这个text.ucs文件,我正在尝试使用python进行解码。
file = open('text.ucs', 'r')
content = file.read()
print content
我的结果是
\\ xf \\ xe \\ x002 \\ 22
我尝试使用utf-16,utf-8进行解码
content.decode('utf-16')
并得到错误
追溯(最近一次通话):文件“”,第1行,文件“ C:\\ Python27 \\ lib \\ encodings \\ utf_16.py”,第16行,解码返回codecs.utf_16_decode(输入,错误,True)UnicodeDecodeError: 'utf16'编解码器无法解码位置32-33中的字节:非法编码
请让我知道我是否遗漏任何东西或我的方法有误
字符串被编码为UTF16-BE(大端),可以这样工作:
content.decode("utf-16-be")
哦,据我所知,您使用的是python 2.xx,但是编码参数仅在python 3.xx中添加,据我所知,我不是python 2.xx的主人,但是您可以在google中搜索io.open,例如,尝试:
file = io.open('text.usc', 'r',encoding='utf-8')
content = file.read()
print content
但是请问您是否需要导入io模块
您可以使用encoding
参数指定要使用的encoding
:
with open('text.ucs', 'r', encoding='utf-16') as f:
text = f.read()
您的字符串需要使用utf-8编码进行未编码,您可以执行我现在所做的解码字符串的操作
f = open('text.usc', 'r',encoding='utf-8')
print f
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.