Python中文
首页
教程
问答
标签
搜索
登录
注册
python:unicode问题
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我正在尝试解码从文件中提取的字符串:</p> <pre><code>file = open ("./Downloads/lamp-post.csv", 'r') data = file.readlines() data[0] </code></pre> <blockquote> <p>'\xff\xfeK\x00e\x00y\x00w\x00o\x00r\x00d\x00\t\x00C\x00o\x00m\x00p\x00e\x00t\x00i\x00t\x00i\x00o\x00n\x00\t\x00G\x00l\x00o\x00b\x00a\x00l\x00 \x00M\x00o\x00n\x00t\x00h\x00l\x00y\x00 \x00S\x00e\x00a\x00r\x00c\x00h\x00e\x00s\x00\t\x00D\x00e\x00c\x00 \x002\x000\x001\x000\x00\t\x00N\x00o\x00v\x00 \x002\x000\x001\x000\x00\t\x00O\x00c\x00t\x00 \x002\x000\x001\x000\x00\t\x00S\x00e\x00p\x00 \x002\x000\x001\x000\x00\t\x00A\x00u\x00g\x00 \x002\x000\x001\x000\x00\t\x00J\x00u\x00l\x00 \x002\x000\x001\x000\x00\t\x00J\x00u\x00n\x00 \x002\x000\x001\x000\x00\t\x00M\x00a\x00y\x00 \x002\x000\x001\x000\x00\t\x00A\x00p\x00r\x00 \x002\x000\x001\x000\x00\t\x00M\x00a\x00r\x00 \x002\x000\x001\x000\x00\t\x00F\x00e\x00b\x00 \x002\x000\x001\x000\x00\t\x00J\x00a\x00n\x00 \x002\x000\x001\x000\x00\t\x00A\x00d\x00 \x00s\x00h\x00a\x00r\x00e\x00\t\x00S\x00e\x00a\x00r\x00c\x00h\x00 \x00s\x00h\x00a\x00r\x00e\x00\t\x00E\x00s\x00t\x00i\x00m\x00a\x00t\x00e\x00d\x00 \x00A\x00v\x00g\x00.\x00 \x00C\x00P\x00C\x00\t\x00E\x00x\x00t\x00r\x00a\x00c\x00t\x00e\x00d\x00 \x00F\x00r\x00o\x00m\x00 \x00W\x00e\x00b\x00 \x00P\x00a\x00g\x00e\x00\t\x00L\x00o\x00c\x00a\x00l\x00 \x00M\x00o\x00n\x00t\x00h\x00l\x00y\x00 \x00S\x00e\x00a\x00r\x00c\x00h\x00e\x00s\x00\n'</p> </blockquote> <p>添加“忽略”并没有真正的帮助…:</p> <blockquote> <p>In [69]: data[2] Out[69]: u'\u6700\u6100\u7200\u6400\u6500\u6e00\u2000\u6c00\u6100\u6d00\u7000\u2000\u7000\u6f00\u7300\u7400\u0900\u3000\u2e00\u3900\u3400\u0900\u3800\u3800\u3000\u0900\u2d00\u0900\u3300\u3200\u3000\u0900\u3300\u3900\u3000\u0900\u3300\u3900\u3000\u0900\u3400\u3800\u3000\u0900\u3500\u3900\u3000\u0900\u3500\u3900\u3000\u0900\u3700\u3200\u3000\u0900\u3700\u3200\u3000\u0900\u3300\u3900\u3000\u0900\u3300\u3200\u3000\u0900\u3200\u3600\u3000\u0900\u2d00\u0900\u2d00\u0900\ua300\u3200\u2e00\u3100\u3800\u0900\u2d00\u0900\u3400\u3800\u3000\u0a00'</p> <p>In [70]: data[2].decode("utf-8", "replace") --------------------------------------------------------------------------- Traceback (most recent call last)</p> <p>/Users/oleg/ in ()</p> <p>/opt/local/lib/python2.5/encodings/utf_8.py in decode(input, errors) 14 15 def decode(input, errors='strict'): ---> 16 return codecs.utf_8_decode(input, errors, True) 17 18 class IncrementalEncoder(codecs.IncrementalEncoder):</p> <p>: 'ascii' codec can't encode characters in position 0-87: ordinal not in range(128)</p> <p>In [71]:</p> </blockquote>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>这个文件是一个UTF-16-LE编码的文件,带有一个初始的BOM。</p> <pre><code>import codecs fp= codecs.open("a", "r", "utf-16") lines= fp.readlines() </code></pre>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
当启用身份验证时,超过一定大小的http发布失败
6 回答
当呈现Flask温度时,bokeh图为空
9 回答
当呈现模板时,如何引用sqlalchemy中的自定义字段?
9 回答
当周围有更多相同类型的标记时,如何从一个标记中提取数据
3 回答
当周数跨越多个y时,如何使用Pandas groupby week
4 回答
当呼唤django和python提示时
10 回答
当命令`Brew installPython@2`然后出现错误“Theme error”。如何解决?
4 回答
当命令[1]不存在时,用户输入命令以列出超出范围的索引
3 回答
当命令/启动被发送到bot时,bot应该删除发送给组的标签;但是bot没有
10 回答
当命令lin执行时,导入datetime会在Python脚本中引发ModuleNotFound“math”
9 回答
当命令中提到Bot时,Bot发送其前缀
8 回答
当命令位于DM中时,Python Discord.py bot将角色分配给服务器中的用户
8 回答
当命令在discord.py中缺少必需的参数时,如何显示消息
3 回答
当命令在多个不同的服务器上处于活动状态时,如何在discord.py上使用while循环
2 回答
当命令在提示符下成功运行时,Python subprocess.check_输出产生错误
5 回答
当命令提示时自动按键按回车键
1 回答
当命令提示符意外关闭时,如何访问python虚拟环境?
10 回答
当命令提示符给出导入错误时,当我尝试运行服务器python文件时,需要进行什么更改?
7 回答
当命令有双引号时,如何从python运行windows命令行命令
4 回答
当命令的一部分来自用户inpu时在linux服务器上执行命令的安全方法
6 回答