python - Write csv with each list as column -
i have dictionary holds 2 level nested list each key looks following:
ordereddict([(0,[['a','b','c','d'],['e','f','g','h']]), (1,[['i','j','k','l'],['m','n','o','p']])])
i write each nested list csv column:
a,b,c,d e,f,g,h i,j,k,l m,n,o,p
the output getting current code is:
['a','b','c','d'] ['e','f','g','h'] ['i','j','k','l'] ['m','n','o','p']
the columns correct remove brackets [ ] , quotes ' '
a,b,c,d e,f,g,h i,j,k,l m,n,o,p
code:
with open('test.csv', 'w', newline='') csv_file: writer = csv.writer(csv_file, quotechar='"', quoting=csv.quote_all) record in my_dict.values(): writer.writerow(record)
any appreciated!
this should work:
with open('test.csv', 'w', newline='') csv_file: writer = csv.writer(csv_file, quotechar='"', quoting=csv.quote_all) record in my_dict.values(): final = [",".join(index) index in record] writer.writerow(final)
Comments
Post a Comment