首页 > 代码库 > python csv文件打开错误:_csv.Error: line contains NULL byte

python csv文件打开错误:_csv.Error: line contains NULL byte

正常的csv文件读取如下:

#coding:utf-8import csvcsvfilename = demo.csvprint u################获取某一行with open(csvfilename, rb) as csvfile:    reader = csv.reader(csvfile)    rows = [row for row in reader]print rows[0], rows[1], rows[2], rows[3]print u################获取某一列with open(csvfilename,rb) as csvfile:    reader = csv.reader(csvfile)    column0 = [row[0] for row in reader]with open(csvfilename, rb) as csvfile:    reader = csv.reader(csvfile)    column1 = [row[2] for row in reader]print column0, column1s = [1,2,3]for i in column0:    print type(i)# print u‘sum:‘,sum(column0)new_column0 = column0.pop(0)print u删除的元素为:, new_column0print u删除后的列表:, column0print type(column0)for i in column0:    print type(i)

 

读取一个技术分享   ucs-2 le 格式(notepa++打开csv)的csv就会报错:  Python CSV error: line contains NULL byte   参考了这个文章里面的内容

https://stackoverflow.com/questions/4166070/python-csv-error-line-contains-null-byte

技术分享

 

代码如下:

#coding:utf-8import csvimport codecstwsfilename = "tws.csv"#读取行print u################获取某一行with codecs.open(twsfilename, rb, "utf-16") as csvfile:    reader = csv.reader(csvfile)    column1 = [row[0] for row in reader]    print column1[0]print u################获取某一列with codecs.open(twsfilename, rb, "utf-16") as csvfile:    reader = csv.reader(csvfile, delimiter=\t)    reader.next()#向下跳一行 这行可以注释掉  主要为了去掉标题行    column1 = [row[1] for row in reader]    print column1    print max(column1)

 

最后感谢大神  参考了很多都搞不定 什么.replace(‘\0‘,‘‘)啊 另存啊 都搞不定  给你几百个这种csv你难道一个个另存啊!

 

python csv文件打开错误:_csv.Error: line contains NULL byte