How do I print a sorted list of a dictionary from a text file?

Viewed 107

so this data file is called friends2.txt:

John,555-234-9876,May 5
Mary,556-987-2367,December 12
Albert,555-987-6765,June 12
Leo,555-789-9865,February 25
Ruth,555-786-1238,October 2
Fred,556-235-4536,June 17

However, I must create the dictionary where the name (ie. John) is the key, and the values are a list of the phone number and birthday (ie. {'John': (555-234-9876, May 5)}. However, I am unsure of how to sort(by key value) and print out every line of the text file one after the other. the aimed output is:

Albert 555-987-6765 June 12
Fred 556-235-4536 June 17
John 555-234-9876 May 5
Leo 555-789-9865 February 25
Mary 556-987-2367 December 12
Ruth 555-786-1238 October 2

So far, my code:

if __name__ == '__main__':
    file_name = "friends2.txt"
    inf = open(file_name,"r")

    myfriends = {}
    # how to build ? myfriends = {key, list(myfriends[name][0], myfriends[name])}
    for line in inf:
        fields = line.split(",")
        myfriends[fields[0]] = fields[1:]

        # value is a list containing the phone number, in position 0, and the birthday in position 1
        # build a list from the phone number and birthday for the "value" in the key-value pair 

    for name in sorted(myfriends):
        print("{} {} {}".format(sorted(myfriends), myfriends[name][0], myfriends[name][1].strip("\n")))

I tried

 for name in sorted(myfriends):
        print("{} {} {}".format(sorted(myfriends), myfriends[name][0], myfriends[name][1].strip("\n")))

but this yields

['Albert', 'Fred', 'John', 'Leo', 'Mary', 'Ruth'] 555-987-6765 June 12
['Albert', 'Fred', 'John', 'Leo', 'Mary', 'Ruth'] 556-235-4536 June 17
['Albert', 'Fred', 'John', 'Leo', 'Mary', 'Ruth'] 555-234-9876 May 5
['Albert', 'Fred', 'John', 'Leo', 'Mary', 'Ruth'] 555-789-9865 February 25
['Albert', 'Fred', 'John', 'Leo', 'Mary', 'Ruth'] 556-987-2367 December 12
['Albert', 'Fred', 'John', 'Leo', 'Mary', 'Ruth'] 555-786-1238 October 2

How would i make it so the key (names) are sorted and iterated properly? Any help would be appreciated!

3 Answers

Try this:

for name in sorted(myfriends):
    print("{} {} {}".format(name, myfriends[name][0], myfriends[name][1].strip("\n")))

You are iterating over keys of the myfriends dictionary which you created. You need to use this key to access the dictionary entry which contains the fields you stored earlier:

for key in sorted(myfriends):
    print("{} {} {}".format(key, myfriends[key][0], myfriends[key][1]))

When storing the entries into the dictionary, you used fields[0] as the key, so you should output key to output the name, and then access myfriends[key][0] and myfriends[key][1] to output what used to be fields[1:].

Let me know if this helps:

file_name = "friends.txt"

friends = []
meta_data = ['name', 'phone', 'dob']
with open(file_name,"r") as f:
    for friend in f.readlines():
        friend_attr = friend.strip().split(",")
        friends.append(dict(zip(meta_data, friend_attr)))


sorted_friends = sorted(friends, key=lambda k: k['name']) 
for f in sorted_friends:
    print("{0},{1},{2}".format(f['name'], f['phone'], f['dob']))

Output:

āžœ  Desktop python stack.py
Albert,555-987-6765,June 12
Fred,556-235-4536,June 17
John,555-234-9876,May 5
Leo,555-789-9865,February 25
Mary,556-987-2367,December 12
Ruth,555-786-1238,October 2
Related