Python Forum

Full Version: Python dictionary with values as list to CSV
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,
I have a dictionary in this way
tdm={
word1: [0,0,0,1.....] -> 253 values in the list
word2: [0,25,0,2.....]
.
.
.
word 12,000: [0,30,0,1.....]
}
I want it to be converted to this form in CSV

TERM | 0 | 1 | 2 | ....... 252 |
------------------------------------|
word1 | 0 | 0 | 0 | ..........
word2 | 0 | 25 | 0 | .........
..
..
word12,000
----------------------------------

Can anyone please help me with this...I'm unable to find a way for more than 5 hrs now
I'm not sure what you need. CVS stands for "Comma-separated values". It's very general. Is it a particular printing format you need, or do you want to save as .CVS?
(Mar-27-2021, 04:22 PM)michael1789 Wrote: [ -> ]I'm not sure what you need. CVS stands for "Comma-separated values". It's very general. Is it a particular printing format you need, or do you want to save as .CVS?
Yes,
I want to save it as .csv output file
I got the answer,
Since no one was able to help with the answer, I will myself post it for someone who might need help with this question.

we just need to convert dictionary to pandas dataframe then apply transpose to it followed by df.to_csv