Question

In: Computer Science

Using Python, Regular Expressions, .map() and other functions as appropriate to format existing address records and...

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

Solutions

Expert Solution

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"]]


Related Solutions

For each of the following functions, find all the minimum SOP expressions using the Karnaugh map....
For each of the following functions, find all the minimum SOP expressions using the Karnaugh map. g(v,x,y,z,w)= Σm(0,1,4,5,8,9,10,15,16,18,19,20,24,26,28,31)       (1 solution)
Regular expressions are used in Python for describing and identifying specific patterns. In Python, we use...
Regular expressions are used in Python for describing and identifying specific patterns. In Python, we use “re” or “Regex” to denote regular expressions. Write the Python Code to return matching analytics word in the following given text. Write the Python Code to return how many times analytics word is provided in this text. Definitions are useful to the extent they serve a purpose. So, is defining analytics important? Yes and no. It’s not likely that we’ll ever arrive at a...
Create and test a python regular expression that matches a street address consisting of a number...
Create and test a python regular expression that matches a street address consisting of a number with one or more digits followed by two words of one or more characters each. The tokens should be separated by one space each, as in 123 Main Street.
Using PROC FORMAT and PROC FREQ for following data: (a) Define an appropriate format for the...
Using PROC FORMAT and PROC FREQ for following data: (a) Define an appropriate format for the gender variable. (b) Produce a 2 X 2 table with gender as the rows and lenses as the columns. (c) Calculate the relative risk and provide a one sentence written interpretation explicitly stating which groups are being compared and defining the outcome. (d) Perform a chi-squared test of association between gender and needing contact lenses. What are the results of the test (i.e. do...
Part 1(15%): Please write the Boolean expressions below in the file, part5_1.js, in the appropriate functions....
Part 1(15%): Please write the Boolean expressions below in the file, part5_1.js, in the appropriate functions. part5_1.js script will execute if you load the part5_1.html file into the browser. Write a Boolean expression that checks that number num, has n digits. Assume that num is an integer.( consider both the positive and negative case for num) Assuming the following are the ingredients for Jamie Oliver's fried rice: 1/2 dozen green onion 1 teaspoon chilli jam 2 cups rice 2 tablespoon...
1) For each of the following functions, find all the minimum SOP expressions using the Karnaugh...
1) For each of the following functions, find all the minimum SOP expressions using the Karnaugh map. a) f(a,b,c) = Σm(0,1,5,6,7) (2 solutions) b) g(v,x,y,z,w)= Σm(0,1,4,5,8,9,10,15,16,18,19,20,24,26,28,31) (1 solution) c) h(a,b,c,d) = Σm(01,2,5,7,9)+Σd(6,8,11,13,14,15) (4 solutions) d) f(a,b,c,d) = Σm(5,7,9,11,13,14)+Σd(2,6,10,12,15) (4 solutions)
For these of string functions, write the code for it in C++ or Python (without using...
For these of string functions, write the code for it in C++ or Python (without using any of thatlanguage's built-in functions) You may assume there is a function to convert Small string into the language string type and a function to convert your language's string type back to Small string type. 1. int [] searchA,ll(string in...str, string sub): returns an array of positions of sub in in...str or an one element array with -1 if sub doesn't exist in in...str
Using a concept map format arrange your identified quality gap into the PDSA cycle. (For bedside...
Using a concept map format arrange your identified quality gap into the PDSA cycle. (For bedside handoff).
Instructions: Using Unix programming language and regular expressions, 1. how many unique ip addresses were seen...
Instructions: Using Unix programming language and regular expressions, 1. how many unique ip addresses were seen note: we only want to look at ipv4 addresses 2. which was most commnly seen ip address on the piece of access.log file below 66.249.75.132 - - [18/Jun/2018:06:41:00 -0500] "GET /~rcoleman/Common/History/Images/?C=N;O=D HTTP/1.1" 200 1976 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" 5.255.250.23 - - [18/Jun/2018:06:41:23 -0500] "GET /~rcoleman/Common/CodeVault/Code/DesignPatterns/Images/DP16-Builder.jpg HTTP/1.1" 304 182 "-" "Mozilla/5.0 (compatible; YandexImages/3.0; +http://yandex.com/bots)" 5.255.250.23 - - [18/Jun/2018:06:41:28 -0500] "GET /~rcoleman/CS121/CourseInfo/Images/WinExp.jpg HTTP/1.1" 304 180...
1. A regular payment to a landlord charged for using an apartment or other housing space...
1. A regular payment to a landlord charged for using an apartment or other housing space is called ___________. rent a cooperative a lease a mortgage loan 2. A contract specifying the legal responsibilities of both the tenant and landlord is known as a __________. power of attorney rental agreement lease mortgage loan 3. Which of the following is NOT considered a a minimum standard of habitability? Running water A washer and dryer Heat A working stove 4. Deciding not...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT