我正在运行以下代码: -
import csv
import sys
from collections import OrderedDict
file_name='sample.txt'
with open(file_name,'rb') as f:
reader = csv.reader(f)
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('sample_out.txt', 'wb') as ofile:
header = ['User_ID','Date','Num_1','Num_2','Com_ID']
extra_headers = sys.argv
header.extend(sys.argv[1:])
n = len(sys.argv)
writer = csv.DictWriter(ofile, fieldnames=header)
writer.writeheader()
col_fill = ''
writer.writerows({col: row_item} for row in p for row_item,col in zip(row+[col_fill]*n,header))
我从命令行传递列名称,例如python script.py BOL1 BOL2 这是输出文件: -
User_ID,Date,Num_1,Num_2,Com_ID,BOL1,BOL1
000101,,,,,,
,04-13-2015,,,,,
,,000012,,,,
,,,000021,,,
,,,,001011,,
,,,,,,
,,,,,,
如何删除额外的逗号并使其可读。
答案 0 :(得分:3)
writer.writerows({col: row_item} for row in p for row_item,col in zip(row+[col_fill]*n,header))
您正在编写仅包含一列的行。只需看看大括号内的内容。也许你打算使用类似的东西:
{col:row_item for row_item,col in zip(row+[col_fill]*n,header)} for row in p
使用每行的信息生成字典。由于我们不需要使用空列填充字典,并且dict接受可重复的键值对,因此可以写为:
dict(zip(header,row)) for row in p