# Write a function that reads the Simpsons phonebook from # https://storage.googleapis.com/cs1111/practice/simpsons_phone_book.txt # The function then finds and returns all phone numbers # of people from Simpsons TV series. # Assuming no area code included. # (hint: use regular expression). # For practice purpose, download the simpsons_phone_book.txt # and write another version of the function that will # read the local file. import re import urllib.request def find_all_phone_number_from_url(url, regex): stream = urllib.request.urlopen(url).read().decode('UTF-8') # get the entire string, find all matches # matches = regex.finditer(stream) matches = regex.findall(stream) phone_list = [] for match in matches: if match not in phone_list: phone_list.append(match) # phone_list.append(match.group()) # print(match.group()) return phone_list url = "https://storage.googleapis.com/cs1111/practice/simpsons_phone_book.txt" regex = re.compile(r"[0-9]?[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]") # raw data, compile print(find_all_phone_number_from_url(url, regex)) #################### # def find_all_phone_number_from_file(filename, regex): # # # # # # # # # # regex = re.compile(r"[0-9]?[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]") # raw data, compile # print(find_all_phone_number_from_file("simpsons_phone_book.txt", regex)) ###################### # Challenging: # Find all possible phone number of people from Simpsons TV series # whose first name start with "J" and last name is "Neu"