In: Computer Science
Using Python, Regular Expressions, .map() and other functions as appropriate to format existing address records and eliminate records with missing critical fields.Critical fieldsincludeFirstName, Lastname, Zipcode+4, and Phone number for customers. For this exercise, create an array to hold data with these 4 fields containing at least 25records. The Zipcode field should contain either traditional 5-digit Zipcode(e.g. 21801)or Zip+4 format(e.g 21801-1101). The phone numbers should contain 10-digit (e.g. 5555555555)or formatted 10-digit(e.g. 555-555-5555). Some records might be corrupt so the data needs to be munged. At this point, we assume only U.S data will be present therefor country code is not needed.
Your python code should label each column, properly format the Zip code to be either 11111 or 11111-1111 formats, properly formatthe phone numbers to always be 111-111-1111 format, and replace incorrect values with a blank string.
Notice invalid data was removed and formatting was applied as required. Commas can be left if desired. Default alignment with the column labels is acceptable.
1.Hardwire the 25 records.(i.e. the user doesn’t have to enter this)
2.Review the code in the textbook examples to simplify this work
3.Use comments to document your code
Summary-
An input array is taken appropriately to cover the four fields of "First name", "Last name", "ZIpcode", and "Phone number".
With the use of regular expressions, the code is executed and tested successfully.
Code- (Executed and tested in Anaconda IDE)
#importing the regular expressions package
import re
#receiving the input
input=[] (Here an input list can be given according to the
required choice)
#list to store the index of a corrupt record
flag=[0 for i in range(len(input))]
#iterating over the input list
for i in range(len(input)):
#First name
a= input[i][0]
#matching the string appropriately
if re.match(r'[A-Za-z]+',a):
flag[i]=1
else:
input[i][0]=""
#Last name
a= input[i][1]
#matching the string appropriately
if re.match(r'[A-Za-z]+',a):
flag[i]=1
else:
input[i][1]=""
#zip code
a= input[i][2]
# matching the zip code of 5 digits and an optional 4 digits
if re.match(r'\d{5}[\-[0-9]{4}]?',a):
flag[i]=1
else:
input[i][2]=""
#Phone number
a=input[i][3]
re.sub("-","",a)
if re.match(r'\d{10}',a):
flag[i]=1
else:
input[i][3]=""
print(input)
Output- (For a particular input of input=[["135","1223","12345-1235","9999999999"],["rahul","kamal","34512-123","666666666"]]