I want to write the dict into csv as this form:
(column1) (column2)
aaa 1
bbb 2
but when I use the '\t' as the delimiter in the following code:
import csv
csv_filename = "test.csv"
dict = {'aaa': 1, 'bbb': 2}
with open(csv_filename, 'wb') as f:
writer = csv.writer(f, delimiter='\t')
for key, value in dict.items():
writer.writerow([key, value])
I could not get the desired ouput, instead the created csv will be like
(column1) (column2)
aaa1
bbb2
You are converting the output into a list which is combining the key and value. Use the following instead.
import csv
csv_filename = "test.csv"
dict = {'aaa': 1, 'bbb': 2}
with open(csv_filename, 'wb') as f:
writer = csv.DictWriter(f, delimiter='\t')
writer.writerows(dict)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With