Reputation: 3411
I have a python script that takes a huge file and copies out only the few columns that I want while also writing a new custom header in the new file.
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)
As I write this file, I need to create an additional column at the beginning of each row with a number, starting at 1 and going to n, so the output looks like this:
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
I'm not sure how I would go about that.
Upvotes: 0
Views: 547
Reputation: 6111
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)
Upvotes: 1
Reputation: 78546
You can use a itertools.count
object for the id
column:
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)
Upvotes: 1