Question
Repeat `Problem 2` but instead of printing the output to a Jupyter notebook cell write the output, line by line, to a file with filename
Repeat `Problem 2` but instead of printing the output to a Jupyter notebook cell write the output, line by line, to a file with filename `output.txt`. Also add code to count the number of lines in the file. Finally, open the file once again, this time to append a string specifying the number of pitchers in the file. A possible string that you can append:
There are 10 pitchers in the file.
Again, don't write this string as given. You need to first find (using Python) the number of rows in the file and then use that information to construct the string that you will be appending to (end of) the file.
Problem 2:
Starting code:
Problem 2 (2 pts): Iterate over lines in the file as demonstrated above and print the following for each line: " 'pitcher' 'has an ERA of' " For example, the first line printed should look like this: NYM pitcher Degrom has an ERA of 1.81 bbfile = open("pitching_stats.txt", "r") for line in bbfile: # TO DO # Split the line into a list of strings.. 1st = line.split() # TO DO # Print the sentence... print(lst[1], "pitcher", lst[0], "has an ERA of", lst[4]). # We close the file bbfile.close() # TO DO # Open the file for writing. outfile = open("output.txt", "w") #print(len(lines)) for line in lines: # TO DO # Split the line into a list of strings.. # TO DO # Write the line to a file... #TO DO: # close the file. #TO DO: # Open the file for appending, construct the string you are going to write to the file # and then write the line to the end of the file. # Don't forget to close the fileStep 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