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

后端 未结 4 727
萌比男神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:31

    You don't need pandas for this, just the simple csv module would work fine.

    import csv
    
    df_out_filename = 'df_out.csv'
    write_headers = True
    with open(df_out_filename, 'wb') as fout:
        writer = csv.writer(fout)
        for filename in allFiles:
            with open(filename) as fin:
                reader = csv.reader(fin)
                headers = reader.next()
                if write_headers:
                    write_headers = False  # Only write headers once.
                    writer.writerow(headers)
                writer.writerows(reader)  # Write all remaining rows.
    

提交回复
热议问题