Concatenating multiple csv files into a single csv with the same header - Python

后端 未结 4 731
萌比男神i
萌比男神i 2020-12-13 21:00

I am currently using the below code to import 6,000 csv files (with headers) and export them into a single csv file (with a single header row).

#import csv f         


        
4条回答
  •  孤城傲影
    2020-12-13 21:13

    Are you required to do this in Python? If you are open to doing this entirely in shell, all you'd need to do is first cat the header row from a randomly selected input .csv file into merged.csv before running your one-liner:

    cat a-randomly-selected-csv-file.csv | head -n1 > merged.csv
    for f in *.csv; do cat "`pwd`/$f" | tail -n +2 >> merged.csv; done 
    

提交回复
热议问题