添加自定義的專欄中,我有一個python腳本,需要一個巨大的文件,並複製出只,我想同時,也寫在新文件中新的自定義標題中的幾列。如何使用csv.writer
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開始,並持續至ñ,因此輸出看起來是這樣的:
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
我不知道我會怎麼做。
是非常有意義的,我只是不知道語法。謝謝! –
@BrianPowell不是問題 – galaxyan
你可以跳過的'我做'爲我+ 1'差一錯誤校正,排在枚舉(R,開始= 1):' –