I am having problems getting my csv file to format correctly when merging it with another. 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:
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=','maybe?lineterminator=','tolineterminator='\n'and now I'm good. Make that into an answer so I can accept it and close this question out. Thanks!