赞
踩
Python csv库读取csv文件经常遇到莫名其妙的编码,报错信息如下:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb1 in position 0: invalid start byte
于是造如下轮子,解析各种编码的csv文件,后续可持续追加各种编码。
如下:
def read_csv(filename):
encodings = ['gbk','utf-8','utf-8-sig','GB2312','gb18030',]
for e in encodings:
data = []
try:
with open(filename, encoding=e) as f:
reader = csv.reader(f)
header = next(reader)
# print(header)
for row in reader:
data.append(row)
print(filename,e)
return data
except:
print(filename,e)
print(filename,"==================")
return False
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。