Question: Write in python Suppose you have a small business and are maintaining customer information in a text file: customerData.txt. Each customer record is on a
Write in python
Suppose you have a small business and are maintaining customer information in a text file: customerData.txt. Each customer record is on a single line with 12 fields separated by commas(,). The order of the fields on a line is: First Name, Middle Initial, Last Name, Street Address, City, State, Zip Code, Country, Email Address, Telephone Number, Gender, and Birthday.
Consider the following program (in file lab6\customers.py) to read each line of text about a customer and generate a list of fields for that customer. The list of fields for each customer is appended to a customers list. Thus, the customers list is a list-of-lists where each item contains the information about a single customer. The general approach taken was to:
read each line from the file (contains all the information about a customer) as a string,
split the line into a list of strings for the fields,
insert this list of strings into the customers list.
def main(): """ Open's file, reads customer information into a list, closes the file"""
custFile = open('customerData.txt','r') customerList = generateList(custFile)
# Echo first and last enter from the customerList
print "customerList[0]:", customerList[0] print "customerList[-1]:",customerList[-1]
custFile.close()
def generateList(custFile): """ Reads customer data from file and returns a list of customers"""
customers = [] for line in custFile:
# Strip the new-line character from the end of the line, then split
# the line on the commas (',') to get a list of customer fields
custInfoList = line.strip().split(',')
customers.append(custInfoList)
return customers main()
From the main function call a new function generateMailingLabels to process the customer list to generate a text file containing mailing labels for only female customers living in Iowa (IA).
The general approach you should take is to:
pass the customerList as a parameter to this function
open the mailing label file (mailingLabels.txt)
the function should loop over all the customer records in the list
if a record satisfies the criteria listed above (female from Iowa), build the mailing label by concatenating select elements from the customer records field list, and
write the mailing label to the file (mailingLabels.txt)
Each mailing label should be formatted as below with 5 blank line separating each label:
Jane Smith 123 Main Street Cedar Falls, IA 50613
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
