import pandas as pd import os #文件路徑注意用\\ inputfilepath = "D:Users/myapp/ccd/" outputfilepath ="D:Users/myapp/ccd/" df_filename = "2020-08-31.csv" columns_list = ['時間', '陽極左', '陽極右', '陰極左', '陰極右', '隔膜左', '隔膜右', '陽極中', '陰極中', '隔膜中', '陰陽中心線差', '陽極寬度', '陰極極寬度', '隔膜寬度', '陽極放卷軸', '陰極放卷軸', '上隔膜放卷軸', '下隔膜放卷軸', '陽左_陰左', '陽左_隔左', 'CPC功能', '插片角度功能'] #先創建一個只有表頭列名的空dataframe,保存為csv df = pd.DataFrame(columns = columns_list) df.to_csv(outputfilepath + df_filename,encoding="utf_8_sig") #遍歷輸入路徑文件夾,用pandas讀取后追加在目標csv文件中 for inputfile in os.listdir(inputfilepath): df = pd.read_csv(inputfilepath+inputfile,encoding='gbk',error_bad_lines=False) df.to_csv(outputfilepath + df_filename, mode="a", index=True,header = False) #查看合並后的表格 pd.read_csv(outputfilepath + df_filename)