简体   繁体   中英

Merge multiple csv files and add a new column

I have a bunch of csv files that i need to merge into one file but with an additional date column

xxxxx20150216.csv

xxxxx20130802.csv

xxxxx20130803.csv

xxxxx20130804.csv

I am using the following code from ( http://cbrownley.wordpress.com/2014/03/09/pythons-voracious-glob-module/ ) to merge them

import csv
import glob
import os
import sys

data_path = ""
outfile_path = "alldata.csv"
filewriter = csv.writer(open(outfile_path,'wb'))
file_counter = 0
for input_file in glob.glob(os.path.join(data_path,'*.csv')):
        with open(input_file,'rU') as csv_file:
                filereader = csv.reader(csv_file)
                if file_counter < 1:
                        for row in filereader:
                                filewriter.writerow(row)
                else:
                        header = next(filereader,None)
                        for row in filereader:
                                filewriter.writerow(row)
        file_counter += 1

But now I need to extract the date from the filename and add it as column along with the other rows. What could be the easiest way to accomplish this?

What about...:

    with open(input_file,'rU') as csv_file:
        filereader = csv.reader(csv_file)
        name, ext = os.path.splitext(input_file)
        date = name[-8:]
        if file_counter < 1:
            for i, row in enumerate(filereader):
                if i==0: row.append('Date')
                else: row.append(date)
                filewriter.writerow(row)
            else:
                header = next(filereader,None)
                for row in filereader:
                    row.append(date)
                    filewriter.writerow(row)

The only tricky part is taking the headers from the first CSV file!-)

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