Search code examples
pythonexport-to-csvdata-dictionary

python data dictionary output


I have a list of key and values in a data dictionary format already

e.g.

{'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}

How can this be put into a csv output where

key1,key2,key3
value1,value2,value3

Solution

  • >>> d = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
    >>> ','.join(d.keys())
    'key3,key2,key1'
    >>> ','.join(d.values())
    'value3,value2,value1'