[英]Merging two csv files in Python
I am having problems getting my csv file to format correctly when merging it with another. 与另一个合并时,我无法将csv文件正确格式化。 I have the following code:
我有以下代码:
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)
Which successfully merges the data I need, however the formatting is incorrect. 可以成功合并我需要的数据,但是格式不正确。 Rather than have the appended data be set to each row and maintaining the format of
list_file
, where x
is the data I am appending: 而不是将附加数据设置为每一行并保持
list_file
的格式,其中x
是我要附加的数据:
col1 col2 col3 col4
a b c x
d e f x
g h i x
I get all data being merged into one row like so: 我将所有数据合并成一行,如下所示:
a b c x d e f x g h i x
Any thoughts on where I am going wrong here? 对我在这里哪里出错有任何想法吗? Thanks!
谢谢!
更改lineterminator=','
也许吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.