# 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') phone_list = [] matches = regex.finditer(stream) for match in matches: print(match) if match.group() not in phone_list: phone_list.append(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): infile = open(filename, "r") # read mode phone_list = [] for line in infile: obj = regex.search(line) # look in the string (line), search for this pattern # if found, search() returns the match object, otherwise None if obj != None: # print(obj, obj.group(), obj.start(), obj.end()) if obj.group() not in phone_list: phone_list.append(obj.group()) infile.close() return phone_list 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))