Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to merge 200 csv files in Python

Guys, I here have 200 separate csv files named from SH (1) to SH (200). I want to merge them into a single csv file. How can I do it?

like image 918
Chuck Avatar asked Mar 25 '10 00:03

Chuck


People also ask

How do I merge multiple CSV files in Python?

To merge all CSV files, use the GLOB module. The os. path. join() method is used inside the concat() to merge the CSV files together.

Can you merge multiple CSV?

The Command Prompt tool, aka CMD, is a Windows Explorer feature; for this feature to work accurately, make sure you have stored only the CSV files you will merge into one Excel file into one folder. 1. Go to the folder storing your CSV files and press down Shift as you right-click the folder and select “Copy path”.


2 Answers

As ghostdog74 said, but this time with headers:

fout=open("out.csv","a") # first file: for line in open("sh1.csv"):     fout.write(line) # now the rest:     for num in range(2,201):     f = open("sh"+str(num)+".csv")     f.next() # skip the header     for line in f:          fout.write(line)     f.close() # not really needed fout.close() 
like image 115
wisty Avatar answered Sep 28 '22 08:09

wisty


Why can't you just sed 1d sh*.csv > merged.csv?

Sometimes you don't even have to use python!

like image 30
blinsay Avatar answered Sep 28 '22 08:09

blinsay