Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

def processFile(filename): # Open file # Read line by line # print the result of the call to processLine(line) pass def processLine(line): # Split line

def processFile(filename): # Open file # Read line by line # print the result of the call to processLine(line) pass

def processLine(line): # Split line by colon # call makeUsername with list item containing string of first and last name # call getAmount with list item containing the string holding amounts # return the formatted string 'Username: name - Account Balance: 000000.00' # Username should be formatted with 7 characters right justified # Acount balance should formatted with 2 decimals left justified # Hint use format string in dicsussed in 9.3. pass

def makeUserName(user_name): # Split on comma # get first name from list # get first character from first name # make the first char upper case # get last name from list # get characters 0->5 from last name # make those characters lowercase # return the two things above concatenated pass

def getAmount(amounts): # split line on comma # loop through amounts # add the floating point values together pass

def main(): filename = input("filename? ") processFile(filename) if __name__ == '__main__': main()

image text in transcribedimage text in transcribed

For today's lab you are going to do some string processing You are going to read a file and process the lines. A single line in the file looks like this FirstName, LastName: 1830.20, 12904.1,-123.89, 594.014 We have provided you with two sample files named sample2.txt and sample1.txt. You can use file read function to read and display the contents of file. You can also create your own file using file write function. You are going to write 4 functions that incrementally process the file 1 processFile() should loop over each line of the file calling processLine for the same and printing the result. 2.processLine() should call makeUsername and getAmount passing the first last name string to makeUsername and the string of numbers to getAmount. You need to use string operation functions to extract the relevant information from a given line of the file. Then it should return the formatted string including the results of the function calls Example: processLine ('FirstName, LastName: 1830.20, 12904.1, -123.89, 594.014) Returns Username: Flastna -Account Balance: 15295.21 NOTE- In the above example, we have passed a string to processLine function, but you should pass a line of the file instead. 3.makeUsername() should take a string with a first and last name separated by a single comma and return a new string with the capitalized first letter of the first name followed by the first 6 characters of the lastName in lower case. Example: makeUsername ('FirstName,LastName Returns Flastna HINT-You can call string split function with different delimiters. Notice, names are being seperated by"."and further each value is seperated by commas" 4.getAmount()should take a string with floating point numbers separated by commas. It should then sum each of the numbers and return the result. Example getAmount ('1830.20, 12904.1,-123.89, 594.014) Returns 15295.21

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Put Your Data To Work 52 Tips And Techniques For Effectively Managing Your Database

Authors: Wes Trochlil

1st Edition

0880343079, 978-0880343077

Students also viewed these Databases questions