如何使用csv.writer添加自定义列

时间:2016-06-29 15:14:04

标签: python csv

我有一个python脚本,它需要一个巨大的文件,只复制我想要的几个列,同时在新文件中写一个新的自定义标题。

import csv,time,string,os, requests

og_file = "\\\\network\\x\\test.csv"
today = time.strftime("%Y-%m-%d")


#fields to keep from original file
fields = ["As Of Date", "Ph", "Home Country"]

with open(og_file) as infile, open("c:\\upload\\output_" + today + ".csv", "wb") as outfile:
    r = csv.DictReader(infile)
    w = csv.DictWriter(outfile, fields, extrasaction="ignore")

    wtr = csv.writer( outfile )    
    wtr.writerow(["upload_date", "phone", "country"])
    for row in r:
        w.writerow(row)

当我编写这个文件时,我需要在每行的开头创建一个附加列,其中包含一个数字,从1开始,然后转到 n ,因此输出如下所示:

id |    upload_date    |    phone    |   country
1        2012-01-01         555-1234      USA
2        2012-02-01         555-1235      USA
3        2012-03-01         555-1236      USA
4        2012-04-01         555-1237      USA

我不确定我会怎么做。

2 个答案:

答案 0 :(得分:1)

fields = ["id"] + fields  # so it'll write later

with open(og_file) as infile, open("c:\\upload\\output_" + today + ".csv", "wb") as outfile:
    r = csv.DictReader(infile)
    w = csv.DictWriter(outfile, fields, extrasaction="ignore")

    wtr = csv.writer( outfile )    
    wtr.writerow(["id","upload_date", "phone", "country"])
    for i,row in enumerate(r, start=1):
        row["id"] = i
        w.writerow(row)

答案 1 :(得分:1)

您可以在itertools.count列中使用id对象:

from itertools import count

with open(og_file) as infile, open("c:\\upload\\output_" + today + ".csv", "wb") as outfile:
    r = csv.DictReader(infile)
    w = csv.DictWriter(outfile, fields, extrasaction="ignore")

    wtr = csv.writer( outfile )    
    wtr.writerow(["id", "upload_date", "phone", "country"])
    c = count(1)
    for row in r:
        row['id'] = next(c)
        w.writerow(row)