在python中合并多个csv文件

时间:2018-12-05 15:36:36

标签: pandas csv dataframe merge

需要帮助合并多个csv文件

import pandas as pd
import glob
import csv
r1=glob.glob("path/*.csv")
wr1 = csv.writer(open("path/merge.csv",'wb'),delimiter = ',')
for files in r1:
    rd=csv.reader(open(files,'r'), delimiter=',')
    for row in rd:
    print(row)
    wr1.writerow(row)

我收到类型错误 TypeError: a bytes-like object is required, not 'str'不知道如何解决这个问题

1 个答案:

答案 0 :(得分:0)

使用pandas可以这样:

dfs = glob.glob('path/*.csv')

result = pd.concat([pd.read_csv(df) for df in dfs], ignore_index=True)

result.to_csv('path/merge.csv', ignore_index=True)