合并多个csv文件并添加新列

时间:2015-03-05 05:23:02

标签: python csv

我有一堆csv文件,我需要合并到一个文件中,但有一个额外的日期列

xxxxx20150216.csv

xxxxx20130802.csv

xxxxx20130803.csv

xxxxx20130804.csv

我正在使用(http://cbrownley.wordpress.com/2014/03/09/pythons-voracious-glob-module/)中的以下代码将它们合并

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

但现在我需要从文件名中提取日期并将其作为列与其他行一起添加。最简单的方法是什么?

1 个答案:

答案 0 :(得分:3)

怎么样......:

    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)

唯一棘手的部分是从第一个CSV文件中获取标题! - )