Information requested:
The company has decided to increase the security of our client's privacy by storing our SSN #s in a separate, encrypted database. The current database needs to have the SSN #s replaced with the format XXX-XX-####, so that the last four digits are still accessible for customer identity verification.
Assignment:
Provide the regex code that would be required to meet the task presented. Describe the relevance of the task: Were regular expressions the best or only way to approach the task? What other data or tasks could be used to increase effectiveness or better achieve the goal presented?
Python Code:
import csv
import re
data = []
with open('customerData.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
data.append(row)
print(data[0])
print(data[1]["Name"])
print(data[2]["Spent Past 30 Days"])
Name | Gender | SSN | Member Date | Phone | State | Zip | Credit Card | Credit Card_Number | Credit Card Exp | Gender | Favorite Radio Station | Purchases Past 30 Days | Spent Past 30 Days | Purchases Past 6 Months | Spent Past 6 Months | Purchases Past 12 Months | Spent Past 12 Months | |
Mamie Mason | female | 459-69-6647 | Mon Aug 01 2016 12:06:01 GMT-0400 (Eastern Daylight Time) | 9472284457 | Indiana | 82439-3619 | [email protected] | Visa | 4263xxxxxxxx1913 | 23-Sep | female | WMMZ | 4 | $2,149.81 | 7 | $5,188.75 | 9 | $10,756.50 |
Dr. Jorge Leonard | male | 267-90-8144 | 10/17/2015 | (482) 962-5537 | Pennsylvania | 44795 | [email protected] | American Express | 3430xxxxxxx6344 | 22-Jun | male | WLRS | 4 | $1,510.68 | 5 | $3,679.59 | 15 | $11,241.27 |
Sue Robertson | female | 431-17-5882 | Fri Jun 03 2016 01:34:30 GMT-0400 (Eastern Daylight Time) | (387) 395-7066 | Virginia | 97198 | [email protected] | American Express | 3446xxxxxxx4624 | 21-Sep | female | WING | 4 | $2,094.67 | 9 | $6,289.29 | 13 | $11,071.11 |
Gilbert Houston | male | 469-80-8860 | Wed May 02 2012 00:45:29 GMT-0400 (Eastern Daylight Time) | 5776043558 | Kentucky | 59108-0390 | [email protected] | Mastercard | 5149xxxxxxxx7215 | 26-Jul | male | WOGV | 0 | $0 | 5 | $3,167.44 | 6 | $6,928.30 |
All the code related explanation is in the code comments. Hope this help!
Code:
import csv
import re
data = []
with open('customerData.csv') as csvfile:
# delimeter changed to tab as the csv file had tab instead of
space
reader = csv.DictReader(csvfile, delimiter='\t')
for row in reader:
data.append(row)
# commented this out for better presentation of the required
output
# print(data[0])
# print(data[1]["Name"])
# print(data[2]["Spent Past 30 Days"])
# loop for all the rows
for row in data:
# the sub() function replaces the regex as first argument
# to string given as second argument in the given string (3rd
argument)
row["SSN"] = re.sub("\d{3}-\d{2}-", "XXX-XX-", row["SSN"])
# print name and ssn for sample output
print(row["Name"], row["SSN"])
Sample output:
Code screenshot:
Were regular expressions the best or only way to approach the task? What other data or tasks could be used to increase effectiveness or better achieve the goal presented?
Regular expressions were the not the best way to approach the task as the data is itself in dictinary form where we can separately acces the SSN of each person. Regular expression first finds the pattern and then replaces. But in our case, the pattern is already found and stored at the "SSN" index => simple string manipulation can do the task, where we simply add XXX-XX- to the last 4 digits like this -
row["SSN"] = "XXX-XX-" + row["SSN"][-4:]
Information requested: The company has decided to increase the security of our client's privacy by storing...
Python programming- Download the following two files you will need for this activity: customerData.csv This file contains randomly generated fictitious customer data. customer_regex.py This is a Python script that imports the customer data into a list of customer details. In your personal playground in Codio, upload the two files and investigate the contents before considering the task you will pose to your peers. Assume the position of a manager of an online retailer. Pose a question to your IT expert...