Link to home
Start Free TrialLog in
Avatar of Wanting2LearnMan
Wanting2LearnManFlag 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
Avatar of gelonida
gelonida
Flag of France image

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

imagine:

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

or

name, peter, lastname, smith
ASKER CERTIFIED SOLUTION
Avatar of gelonida
gelonida
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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