Python Forum
Python dictionary with values as list to CSV - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Python dictionary with values as list to CSV (/thread-33079.html)



Python dictionary with values as list to CSV - Sritej26 - Mar-27-2021

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


RE: Python dictionary with values as list to CSV - michael1789 - Mar-27-2021

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?


RE: Python dictionary with values as list to CSV - Sritej26 - Mar-27-2021

(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?



RE: Python dictionary with values as list to CSV - Sritej26 - Mar-27-2021

Yes,
I want to save it as .csv output file


RE: Python dictionary with values as list to CSV - Sritej26 - Mar-27-2021

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