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

后端 未结 2 488
情深已故
情深已故 2021-01-21 20:07

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

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

So, that the output

2条回答
  •  梦毁少年i
    2021-01-21 20:35

    maxDict = {'test1': ('alpha', 2), 'test2': ('gamma', 2)}
    csvData = []
    for col1, (col2, col3) in maxDict.iteritems():
      csvData.append("%s, %s, %s" % (col1, col2, col3))
    f = open('test.csv', 'w')
    f.write("\n".join(csvData))
    f.close()
    

提交回复
热议问题