python - Turning text in a text file to lists in a dictionary -


so example had text file peoples phone numbers,name,address. looked return @ end of each line

555-667282,bill higs,67 hilltop  555-328382,john paul,85 big road  555-457645,zac fry,45 tony's rd  555-457645,kim fry,45 tony's rd 

and wanted put in dictionary , in dictionary phone number key , there name , address list. if wanted print dictionary this. code this

{555-667282: ['bill higs','67 hilltop'], 555-328382: ['john paul','85 big road'], 555-457645: ['zac fry','45 tony's rd'],['kim fry','45 tony's rd']}

dicto = {}  open('your_file.txt') f:     line in f:         s_line = line.rstrip().split(',')         dicto[s_line[0]] = s_line[1:] 

edit:

to handle cases there multiple entries associated 1 phone number:

from collections import defaultdict  dicto = defaultdict(list)  open('your_file.txt') f:     line in f:         s_line = line.rstrip().split(',')         dicto[s_line[0]].append(s_line[1:]) 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -