Python 读取csv文件提示“line contains NULL byte”错误

当python读取文件出现_csv.Error: line contains NULL byte时,

复制代码
# -*- coding:utf-8 -*-

import csv

with open(r'E:\abc\web_test\userinfo.csv','rb') as f:
    reader = csv.reader(f)
    rows = [row for row in reader]
    print rows
复制代码

报错:

复制代码
D:\Python27\python.exe E:/abc/loop_reader.py
Traceback (most recent call last):
  File "E:/abc/web_test/loop_reader.py", line 7, in <module>
    rows = [row for row in reader]
_csv.Error: line contains NULL byte

Process finished with exit code 1
复制代码

错误提示为:_csv.Error: line contains NULL byte

翻译为:CSV错误,行包含空字节

原因:通常是因为保存时扩展名为xls或xlsx,而将其改为csv文件通常是重命名

解决方法:把它另存为.csv文件即可

posted @ 2018-01-23 16:49  MatthewLu  阅读(1868)  评论(0编辑  收藏  举报