Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please Note: You need to use try except block wherever the need arises for any o

ID: 3753477 • Letter: P

Question

Please Note: You need to use try except block wherever the need arises for any of the following questions. You would also need to log the exceptions. 1. Your task is to write a function to print valid phone numbers. You need to accept the inputs from the file(inputl.txt) and output the valid phone numbers on the console. Format for valid phone numbers is: where x would be a number. Sample Input: input1.bxt Example: Input: 987-123-4567 123 456 7890 (123) 456-7890 Output: 987-123-4567 (123) 456-7890 Hint: You may use regular expressions. (import re)

Explanation / Answer

# filename variables filename = 'input1.txt' newfilename = 'result.txt' # read the file if os.path.exists(filename): data = open(filename,'r') bulknumbers = data.read() else: print "File not found." raise SystemExit # regex = 000-000-0000/(000)000-0000 r = re.compile(r' (d{3}[-.s]??d{3}[-.s]??d{4}|(d{3})s*d{3}[-.s]??d{4}|d{3}[-.s]??d{4}) ') results = r.findall(bulknumbers) numbers = "" for x in results: numbers += str(x)+" " # function to write file def writefile(): f = open(newfilename, 'w') f.write(numbers) f.close() print "File written."