Avatar of Wanting2LearnMan
Flag for United Kingdom of Great Britain and Northern Ireland asked on

Convert python dictionary to csv string.

I have a dictionary that I need to convert to a csv string.

The dictionary consists of [key, value, key, value] etc etc

How can I get all the values in the dictionary and convert it to a cvs string??

I have tried:

myCsvString = ','.join(myDict)

But I get the error:
Exception Type: TypeError
Exception Value:

sequence item 0: expected string, dict found

PLease help
PythonProgrammingScripting Languages

Avatar of undefined
Last Comment

8/22/2022 - Mon

Without context and knowing exactly what you want to achieve ti's difficult to know.


a={'lastname': 'smith', 'name': 'peter'}

Open in new window

what would you expect as output?

lastname, smith, name, peter

Please note, that even the order wouldn't be guaranteed it could be either

lastname, smith, name, peter


name, peter, lastname, smith

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

Please note, that there is a module called csv which takes care about all the proper escaping and about the different styles of csv formats.

However you should first describe exactlyhow you'd like to have your dict converted to a csv line / file

The modules doc: http://docs.python.org/library/csv.html
Your help has saved me hundreds of hours of internet surfing.