2011-01-19 127 views
6

我试图解码字串我从文件了:蟒蛇:unicode的问题

file = open ("./Downloads/lamp-post.csv", 'r') 
data = file.readlines() 
data[0] 

“\ XFF \ xfeK \ x00e \ x00y \ x00w \ x00o \ x00r \ x00d \ X00 \ t \ x00c \ x00o \ x00t \ x00i \ x00t \ x00i \ x00o \ x00n \ x00t \ x00o \ x00l \ x00l \ x00o \ x00o \ x00l \ x00l \ x00o \ x00o \ x00l \ x00y \ 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 \ xOO \ 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 \ x00n \ x00 \ x00s \ x00h \ x00a \ x00r \ x00e \ x00 \ x00 \ x00r \ x00e \ x00 \ x00a \ x00r \ x00e \ x00 \ x00s \ x00c \ x00c \ x00c \ x00s \ x00c \ x00c \ x00c \ x00s \ x00c \ x00c \ x00s \ x00c \ x00c \ x00c \ x00c \ x00c \ x00c \ x00c \ x00 \ x00A \ x00v \ x00g \ x00。\ x00 \ x00C \ x00P \ x00C \ x00 \ t \ x 00E \ x00x \ x00c \ x00t \ x00e \ x00d \ x00d \ x00t \ x00e \ x00d \ x00P \ x00a \ x00g \ x00e \ x00 \ x00c \ x00r \ x00r \ x00o \ x00a \ x00g \ x00e \ x00 \ t \ x00c \ x00a \ x00l \ x00c \ x00c \ x00c \ x00c \ x00c \ x00c \ x00c \ x00c \ x00c \ x00c \ x00s \ x00c \ x00c \ x00c \ x00s \ x00 \ n '

添加忽略不真正帮助...:

在[69]:数据[2] 出[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 \在[70]中:data [2] .decode(“utf-8”,“utf-8”), “替换”) -------------------------------------------- ------------------------------- 回溯(最近通话最后)

/Users/oleg/in ()

/opt/local/lib/python2.5/encodings/utf_8.py 在解码(输入,错误) 15 DEF解码(输入,误差= '严格'): ---> 16级返回的编解码器.utf_8_decode(输入,错误, True) 18类IncrementalEncoder(编解码器。IncrementalEncoder):

: 'ASCII' 编解码器不能在0-87位编码字符 :顺序不在 范围(128)

在[71]:

+0

我的答案没有错误。但它取决于你想忽略或替换不可解码的字符。 – orlp 2011-01-19 13:21:07

回答

14

这看起来像UTF-16的数据。因此,尝试

data[0].rstrip("\n").decode("utf-16") 

Edit(对于您的更新):尝试将整个文件同时解码,即

data = open(...).read() 
data.decode("utf-16") 

的问题是,在UTF-16的换行符是“\ n \ x00“,但使用readlines()将在”\ n“处拆分,而在下一行留下”\ x00“字符。

3

EDIT

既然你发布2.7,这是2.7的解决方案:

file = open("./Downloads/lamp-post.csv", "r") 
data = [line.decode("utf-16", "replace") for line in file] 

忽略undecodeable字符:

file = open("./Downloads/lamp-post.csv", "r") 
data = [line.decode("utf-16", "ignore") for line in file] 
+0

In [21]:file = open(“./Downloads/lamp-post.csv”,'r') In [22]:data = [line.decode()for line in file] --- -------------------------------------------------- ---------------------- Traceback(最近呼叫的最后一个) /Users/oleg/ in在[23]:data = [line.decode())中,'ascii'编码解码器不能解码位置0中的字节0xff:序号不在范围内(128) 对于文件中的行] – 2011-01-19 13:10:27

+0

呵呵,你想忽略这些无效字符还是替换它们?假设接替编辑我的答案。 – orlp 2011-01-19 13:11:43

+0

或UTF-16。 _15char_ – orlp 2011-01-19 13:12:49

4

该文件是一个UTF-16-LE编码文件,带有初始BOM。

import codecs 

fp= codecs.open("a", "r", "utf-16") 
lines= fp.readlines()