Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Program Instructions Part 1 B: 1 . First, you will create the starting data text file. Open Notepad ( or any other plain text editor
Program InstructionsPart B:
First, you will create the starting data text file.
Open Notepad or any other plain text editor and add the following data to the file exactly as shown:
Morrison,Marion, Mockingbird Lane,Atlanta,GA
Jane,Mary, Riverside Drive,Hill Valley,CA
Rubble,Barney, Beacon Street,Boston,MA
Davis,Betty, Stigwood Avenue,New York,NY
Save the file as ITUData.txt
Define program variables. You will read the records from this text file into a list in your program. Define a list variable to hold the data ie recordsList Define and initialize a record count variable to be used later ie recordCount
Reference: Refer back to your readings from Learn to Program With Python : A StepbyStep Guide to Programming, nd edition, Chapter : Lists.
Define a file handle and open your text data file for reading.
Hint: If you want to reference the file by its name using a relative file path like ITUData.txt this file must exist in the same folder with your Python source code py file. Otherwise, you will need to include the full file path, such as C:MyFolderMySubFolderMySubSubFolderMyFiletxt
Reference: Learn to Program With Python : A StepbyStep Guide to Programming, nd edition, Chapter : Defining a Path to a File Reading From and Writing to a File File Handle.
Use a for loop to access each record in the file and append each record to the list variable each iteration.
Hint: Use strip to remove any line breaks like recordsList.appendlinestrip
Reference: Learn to Program With Python : A StepbyStep Guide to Programming, nd edition, Chapter : Writing and Reading a Line at a Time With a File.
Close the file.
Reference: Learn to Program With Python : A StepbyStep Guide to Programming, nd edition, Chapter : Defining a Path to a File Reading From and Writing to a FileFile Handle.
Append the following string of data to the list exactly as shown:
Brady,Bobby, Clinton Way,Los Angeles,CA
Hint: recordsList.appendBrady,Bobby, Clinton Way,Los Angeles,CA
Open a new csv file for writing. Name this file ITUPromoCredit.csv
Reference: Learn to Program With Python : A StepbyStep Guide to Programming, nd edition, Chapter : Defining a Path to a File Reading From and Writing to a File File Handle.
Write a record to the file that contains the following values that will serve as the header record in your csv file:
Customer ID Last Name, First Name, Address, City, State, Promo Credit
Hint: add
to the end of this string to force a new line.
Reference: Learn to Program With Python : A StepbyStep Guide to Programming, nd edition, Chapter : Defining a Path to a File Reading From and Writing to a File File Handle.
Use another for loop to process the list data.
With each iteration through the loop, write out a record.
Append the $ promotional credit.
Hint: This can be done by simply adding the value in a string like file.write$
Dont forget the newline character at the end.
Increment the record count variable.
Close the file.
Conclude the program by printing a statement that includes the record count:
There were records written to the promo credits csv file.
Summary of files used in this program:
INPUT: ITUData.txt
Morrison,Marion, Mockingbird Lane,Atlanta,GA
Jane,Mary, Riverside Drive,Hill Valley,CA
Rubble,Barney, Beacon Street,Boston,MA
Davis,Betty, Stigwood Avenue,New York,NY
OUTPUT: ITUPromoCredit.csv
This table represents the csv file opened in a spreadsheet application
Customer ID
Last Name
First Name
Address
City
State
Promo Credit
Morrison
Marion
Mockingbird Lane
Atlanta
GA
$
Jane
Mary
Riverside Drive
Hill Valley
CA
$
Rubble
Barney
Beacon Street
Boston
MA
$
Davis
Betty
Stigwood Avenue
New York
NY
$
Brady
Bobby
Clinton Way
Los Angeles
CA
$
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started