# Write a function that reads a file named simpsons_phone_book.csv # and returns a dictionary containing # all Simpsons' characters/places and their phone numbers. # # Each character/place may have multiple phone numbers. # # Note: you should consider how you'd handle data with extra spaces # let's write down steps we'll be doing # open the file # read line by line # strip (spaces, \n), each line # split(',') # take care of extra spaces --> strip that piece/column of data # get each line in a list containing name, phone number # if name in dict, add phone number # otherwise, add name , phone number to dict def find_all_phone_numbers(filename): phone_dict = {} infile = open(filename, "r") # line_no = 1 for line in infile: data = line.strip().split(",") # verify if we get the correct data # print(line_no, ":", data[0],":", data[1]) # have extra spaces # print(line_no, ":", data[0].strip(),":", data[1].strip()) # take care of extra spaces # line_no += 1 name = data[0].strip() phone = data[1].strip() if name not in phone_dict.keys(): phone_dict[name] = phone else: for k, v in phone_dict.items(): # items() return key and value if name == k: phone_dict[k] = v + "," + phone # update/add phone for that person infile.close() return phone_dict print(find_all_phone_numbers("simpsons_phone_book.csv"))