使用pandas合并结构相同的csv文件
#encoding=utf-8 import pandas as pd import os import csv def concat_csv(filename,dirpath): with open(filename,'w') as f: cw=csv.writer(f) cw.writerow(['column1','column2']) for inputfile in os.listdir(dirpath): order=['column1','column2'] df=pd.read_csv(dirpath+inputfile,encoding='utf-8-sig') try: # 对字段进行排序 df=df[order] df.to_csv(filename, mode='a', index=False, header=False,encoding='utf-8-sig') except Exception as e: print(e,inputfile,df.columns) if __name__=='__main__': filename='result.csv' dirpath='d:/' concat_csv(filename,dirpath)