简体   繁体   中英

Merge CSV files with python

I have a folder of around 200 CSV files. Each only have values in the first row. How do I combine the files so that is each CSV file becomes a row in the new dataset.

For example:

1.csv contains:
[1, 2, 3, 4, 5, 6]
2.csv contains:
[2, 3, 4, 5, 5, 7]

I want to combine the CSV's so that the final CSV looks like:

final.csv contains:
[1, 2, 3, 4, 5, 6]
[2, 3, 4, 5, 5, 6]

*Each matrix is a row of the .csv, and each , means a new cell.

Thank You!

If you have bash, you can use

cat *.csv > combined.csv

Or, if you want to do it the python way:

import csv

with open('combined.csv', 'wb') as out_file:
    writer = csv.writer(out_file)
    for fname in os.listdir('.'):
        with open(fname, 'rb') as in_file:
            for row in csv.reader(in_file):
                writer.writerow(row)

Here you'll need to navigate to the directory containing your 200 odd csv files.

  • windows cmd - type copy *.csv combined.csv

  • linux bash - cat file1.csv file2.csv > combined.csv

  • python - something like

     fout=open("out.csv","a") for num in range(1,201): for line in open("sh"+str(num)+".csv"): fout.write(line) fout.close() 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM