【问题标题】:Merge multiple csv files in python在python中合并多个csv文件
【发布时间】:2019-05-07 05:26:49
【问题描述】:

在合并多个 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' 不知道如何解决这个问题

【问题讨论】:

  • 我猜你需要在w模式下打开文件,而不是wb模式
  • 感谢好奇的回复...当我将其更改为 w TypeError: '_csv.writer' object is not subscriptable 时出现以下错误
  • _csv.writer 错误是怎么回事?顺便说一句,最后两行有一个缩进错误。

标签: pandas csv dataframe merge


【解决方案1】:

使用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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-11
    • 1970-01-01
    • 2020-09-01
    • 1970-01-01
    • 2012-04-29
    • 2018-10-20
    • 2016-01-26
    相关资源
    最近更新 更多