Wanting2LearnMan
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
imagine:
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