在将csv文件与另一个文件合并时,我无法正确格式化。我有以下代码:
list_dir = glob.glob('C:/.../*')
imprint = 'C:/.../imprint_report.csv'
export_dir = 'C:/.../Export//'
for imprint_d in csv.DictReader(open(imprint)):
for list_file in list_dir:
list_token = os.path.basename(list_file)[:6]
if imprint_d['token'] == list_token:
v = open(list_file, 'rb')
r = csv.reader(v, delimiter='\t')
row0 = r.next()
row0.append('token')
all_rows = []
for item in r:
item.append(imprint_d['token'])
all_rows.append(item)
#print item
output = open(export_dir+list_token+'_append.csv', 'wb')
writer = csv.writer(output, lineterminator=',')
writer.writerows(all_rows)
哪个成功合并了我需要的数据,但格式不正确。而不是将附加数据设置为每一行并保持list_file
的格式,其中x
是我要追加的数据:
col1 col2 col3 col4
a b c x
d e f x
g h i x
我将所有数据合并为一行,如下所示:
a b c x d e f x g h i x
关于我在哪里出错的任何想法?谢谢!
答案 0 :(得分:2)
可能更改lineterminator=','
?