I have nested dictionary (with length > 70.000):
users_item = {
"sessionId1": {
"12345645647": 1.0,
"9798654": 5.0
},
"sessionId2":{
"3445657657": 1.0
},
"sessionId3": {
"87967976": 5.0,
"35325626436": 1.0,
"126789435": 1.0,
"72139856": 5.0
},
"sessionId4": {
"4582317": 1.0
}
......
}
I want create CSV file from my nested dictionary, my result will look like:
sessionId1 item rating
sessionId1 item rating
sessionId2 item rating
sessionId3 item rating
sessionId3 item rating
sessionId3 item rating
sessionId3 item rating
.......
I found this post: Convert Nested Dictionary to CSV Table
It's similar to my question but it's not working when I try all answers, pandas library run out of memory
How I can make CSV file with my data?
Just loop through the dictionary and use the Python csv writer to write to the csv file.
with open('output.csv', 'w') as csv_file:
csvwriter = csv.writer(csv_file, delimiter='\t')
for session in users_item:
for item in users_item[session]:
csvwriter.writerow([session, item, users_item[session][item]])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With