读csv文件的时候发现编码方式同一个文件夹里面好几种,于是干脆就写一段代码让他能支持这几个。通过判断UnicodeDecodeError错误码来处理异常
for root, dirs, files in os.walk(dir):
for file in files:
print(os.path.join(root, file))
fileaddr = os.path.join(root, file)
if(file.endswith('csv')==False):
continue
if(file.endswith('csv')):
try:
orderdf = pd.read_csv(fileaddr, encoding='GB18030')
except UnicodeDecodeError:
try:
orderdf = pd.read_csv(fileaddr, encoding='utf8')
except UnicodeDecodeError:
orderdf = pd.read_csv(fileaddr, encoding='iso-8859-1')