seperate dictionary and store key sepertely in tsv file

59 views
Skip to first unread message

yinx

unread,
Dec 1, 2015, 5:45:11 AM12/1/15
to Python GCU Forum
Hi all!

I have a dictionary like this: {group 1: [1,2,4,5,6,7], Group 2:[8,3,9,11,13,16], Group 3:[10,12,14,17,18]}
I would like to save them in seperate files where you would seperate them by their keys, so the group.
Can anybody explain to me how to do this? I am only able to store the dictionary in one file, but then it would only give me all the keys and their values in one row.

Thank you a lot!

Robert Mandić

unread,
Dec 1, 2015, 6:43:17 AM12/1/15
to Python GCU Forum
d = {'g3': [10, 12, 14, 17, 18], 'g2': [8, 3, 9, 11, 13, 16], 'g1': [1, 2, 4, 5, 6, 7]}

for k,v in d.items():
    lines = '\n'.join(map(str,v))
    open(k, 'w').writelines(lines)


--
You received this message because you are subscribed to the Google Groups "Python GCU Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-gcu-for...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Lp, Robert

yinx

unread,
Dec 1, 2015, 7:17:19 AM12/1/15
to Python GCU Forum
Thank you for your answer! but I still have a few questions.
The code that you gave me only stores one of the keys with it's value and it gives me a file with all the values without telling me which key it is.
How do I make files of every key and know which key it is?


Op dinsdag 1 december 2015 11:45:11 UTC+1 schreef yinx:

Robert Mandić

unread,
Dec 1, 2015, 7:32:49 AM12/1/15
to Python GCU Forum
The name of the file is the name of the key.
Is that not good enough?

--
You received this message because you are subscribed to the Google Groups "Python GCU Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-gcu-for...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Lp, Robert
Reply all
Reply to author
Forward
0 new messages