Python-pandas库 读取Excel文件数据的常见错误集合

问题1
Error tokenizing data. C error: Buffer overflow caught - possible malformed input file.

解决方案
这一般是缓冲区溢出错误,造成这种错误的原因是.csv文件中每行使用了 \r ,也就是回车符。
解决方案就是给 read_csv 添加参数 lineterminator=”\n” , 指定用“\n” 作为换行符。

df = pd.read_excel(r'test.csv',lineterminator="\n")

问题2
“pandas.parser.CParserError: Error tokenizing data. C error: Expected 2 fields in line 3, s”

解决方案
加入参数error_bad_lines=False

df = pd.read_excel(r'test.csv',error_bad_lines=False)

问题3
‘utf-8’ codec can’t decode byte 0xd0 in position 0: invalid continuation byte

解决方案
使用国标码编码 encoding = ‘gb2312’

df = pd.read_excel(r'test.csv',encoding = 'gb2312')

如果依旧报错或者’gb2312’报错,可以选择编码范围更广的‘gb18030’

encoding='gb18030'

如果还不能解决,说明文中出现了连‘gb18030’也无法编码的字符,可以使用‘ignore’属性忽略非法字符,path为文件路径。

df = open(path, encoding='gb18030', errors='ignore')

或者

df=open(path).read().decode(‘gb18030’,’ignore’)

如果errors报错了(parser_f() got an unexpected keyword argument ‘errors’),请升级你的pandas版本。

问题4
index 1 is out of bounds for axis 1 with size 1

扫描二维码关注公众号,回复: 11190908 查看本文章

解决方案
数据中有空值,请检查文件里的内容。把无关数据删除,空值补上数据。

问题5
Error: ‘gbk’ codec can’t decode byte 0x80 in position 205: illegal multibyte sequence"

解决方案
后面加上参数:‘rb’

df = pd.read_excel(r'test.csv','rb')

问题6
读取数据时总是少一行数据。

解决方案
加上参数header = 0或者header = None。

df = pd.read_excel(r'test.csv',header = None)
原创文章 41 获赞 65 访问量 8360

猜你喜欢

转载自blog.csdn.net/weixin_44436677/article/details/106089238
今日推荐