Question: 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

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 line 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 separated by ":" and further each value is separated 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

Provided Code:

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()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!