1

Need help with merging multiple csv file

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)

I am getting a type error TypeError: a bytes-like object is required, not 'str' not sure how to resolve this

3
  • 1
    I guess you need to open the file in w mode only not wb mode Commented Dec 5, 2018 at 15:43
  • Thanks curious mind for the reply...i got the below error when i changed it to w TypeError: '_csv.writer' object is not subscriptable Commented Dec 5, 2018 at 16:38
  • What is the line with the _csv.writer error? By the way there is an indent error in the last 2 lines. Commented Dec 5, 2018 at 16:42

1 Answer 1

1

Using pandas you can do it like this:

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)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.