python - 无法使用 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(input, errors, True) UnicodeDecodeError: “utf16”编解码器无法解码位置 32-33 中的字节:非法编码
如果我遗漏任何东西或我的方法有误,请告诉我
解决方案
哦,据我了解,您使用的是 python 2.xx,但编码参数仅在 python 3.xx 中添加,据我所知,我不是 python 2.xx 的大师,但您可以在谷歌中搜索 io.open 例如尝试:
file = io.open('text.usc', 'r',encoding='utf-8')
content = file.read()
print content
但是您是否需要导入 io 模块
推荐阅读
- python - 使用 Scrapy 遍历链接
- python - 仅对 else 停止代码 30 秒
- c# - C# 列表/字典添加方法导致 StackOverflowException
- javascript - 如何使用命令将电报组中的链接放入机器人
- javascript - 在前端跟踪已删除对象并将这些更改保存在服务器上的最有效方法是什么?
- python - 为什么数据框输入和数字标准输入之间存在不一致?
- php - 转发到 nginx 上的节点端口后,有哪些解决方案可用于运行 php
- python - 更新pyqtgraph的最佳方法?
- python - 您能否建议一种更 Pythonic 的方法来查找并返回具有特定属性值的列表元素
- flutter - Flutter:让 ListViewBuilder 中的图像在新行溢出