下面的代碼的輸出加: -如何新列(頭)爲CSV文件通過命令行參數
import datetime
import csv
file_name='sample.txt'
with open(file_name,'rb') as f:
reader = csv.reader(f,delimiter=",")
#headers = reader.next()
p=[]
for row in reader:
row[0] = row[0].zfill(6)
row[2] = row[2].zfill(6)
row[3] = row[3].zfill(6)
row[4] = row[4].zfill(6)
row[1] = row[1][5:7] + "-" + row[1][8:10] + "-" + row[1][:4]
p.append(row[:5])
print p
with open('names.txt', 'wb') as ofile:
writer = csv.writer(ofile)
for row in p:
writer.writerow(row)
是以下幾點: -
User_ID,--Date,0Num_1,0Num_2,Com_ID
000101,04-13-2015,000012,000021,001011
000102,04-03-2014,000001,000007,001002
000103,06-05-2013,000003,000004,000034
000104,12-31-2012,000004,000009,001023
000105,09-09-2011,000009,000005,000104
我要添加從命令行將新列添加到csv文件。 例如python script_name.py Dept_ID位置將爲Comp_ID旁邊的Dept_ID和Location創建列。
任何人都可以在這裏指導我!
可能重複(http://stackoverflow.com/questions/20347766/pythonically-add-header-to-a-csv-file) –
你會如何填寫它? –
對於命令行選項,請查看getopt。 https://docs.python.org/2/library/getopt.html – harandk