错误:_csv.Error: line contains NULL byte(已解决)

原代码如下

file = open("/Users/apple/Downloads/data.csv","rb")
file = csv.reader(file)
for i in file:
    print i

错误

错误

原因

文件由excel转.csv文件得来

解决方法

既然是excel,那么用excel包导入就可了嘛~╮( ̄▽ ̄”“)╭
import xlrd

修改代码如下

import xlrd

file = xlrd.open_workbook("/Users/apple/Downloads/data.csv")
sh = file.sheet_by_name("Sheet1")
nrows = sh.nrows
for i in range(1,10):
    print sh.row_values(i)

顺便笔记下excel读取某个单元格函数:

cell_value = file.cell_value(1,1)
#获取一行一列数值

猜你喜欢

转载自blog.csdn.net/github_38236333/article/details/70188412