Categories
Development Python

How can I convert a dictionary into a comma separated .txt file while maintaining the key:value pair? Python

I have a dictionary that I need to write in a .txt file in comma separated value format. I have tried

mydict = {'first': ['1','2','3'], 'second': ['2','3','4'], 'third': ['3','4','5']}
f = open("doc.txt","w")
for key, values in mydict:
    f.write(','.format(key, values)+"\n")
f.close()

But it does not work , as the values are too many.
I have also tried to create two comma separated lists for keys and values, using .join, but I need the values to be mapped to the keys, as you would have in a CSV file.

Thank you for your help!

Leave a Reply

Your email address will not be published. Required fields are marked *