Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: How to write a dictionary of tuple values to a csv file?

How do I print the following dictionary into a csv file?

maxDict = {'test1': ('alpha', 2), 'test2': ('gamma', 2)} 

So, that the output CSV looks as follows:

test1, alpha, 2
test2, gamma, 2
like image 283
siva Avatar asked Aug 31 '25 20:08

siva


1 Answers

import csv
with open("data.csv", "wb") as f:
    csv.writer(f).writerows((k,) + v for k, v in maxDict.iteritems())
like image 154
Sven Marnach Avatar answered Sep 04 '25 09:09

Sven Marnach