how to merge 200 csv files in Python
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 neededfout.close()
Why can't you just sed 1d sh*.csv > merged.csv
?
Sometimes you don't even have to use python!
Use accepted StackOverflow answer to create a list of csv files that you want to append and then run this code:
import pandas as pdcombined_csv = pd.concat( [ pd.read_csv(f) for f in filenames ] )
And if you want to export it to a single csv file, use this:
combined_csv.to_csv( "combined_csv.csv", index=False )