Question: import sys def read _ book ( filename ) : contents = [ ] with open ( filename ) as book: for line in book.readlines

import sys
def read_book(filename):
contents =[]
with open(filename) as book:
for line in book.readlines():
line = line.strip('
') # Strip off the trailing new-line character
line = line.split('|') # Split the line into separate fields
line[1]= int(line[1])
contents.append(line)
contents.sort(key=lambda x: x[1])
return contents
def find_longest_line(contents):
long_line =["",0]
for i, line in enumerate(contents):
if len(line[0])> len(long_line[0]):
long_line =[line[0],i]
return long_line
def calculate_average_length(contents):
total_length = sum(len(line[0]) for line in contents)
average_length = round(total_length / len(contents))
return average_length
def write_results(book_name, contents):
filename= f"{filename}_book.txt"
with open("filename","w") as summary_file:
summary_file.write(TTL+"
")
longest_line= find_longest_line(contents)
summary_file.write(f" Longest line ({longest[0]})): {longest[0]}|{longest[1]}
")
average_length = calculate_average_length(contents)
summary_file.write(f"Average length: {average_length}
")
for line in contents:
summary_file.write(f"{line[0]}n")
def main():
filename = sys.argv[1][:-4]
contents = read_book(sys.argv[1])
write_results(filename, contents)
if __name__=="__main__":
main()
Structure
File
library.py
Input
a text file such as TTC.txt
one input file for each book
Output
text file such asTTC_book.txt
one output file for each book
Problem
Youre an intern at the Library of Congress. You are given a collection of text files where each file corresponds to a book. Each file is named with a three-letter code for the book it contains. Examples are like TTC.txt, WOO.txt, TTL.txt and other names too. There could be many such input files and we may not know how many.
Each line in the file looks like this: one line from the book and the line number, separated by a '|. For example: "It was the best of times, it was the worst of times,"|1
You discover though, that the content of each file is scrambled so that the lines are not in order.
Your task is to write a program that reads each of these book files and does a few things:
Find the longest line from the book along with its line number.
Calculate the average length of the lines in the book.
Unscramble the lines of text in order of line number.
Once your program has done that, it needs to save this information in a new file called '_book.txt. The format would be:
first line is the three-letter code
second line is the longest line with line number and content as shown
third line is the average length
lines 4 to end are the contents of the book in line order
Example Run
As an example, for this input
python3 library.py TTL.txt
Your code will generate a file named as TTL_book.txt and the first few lines of that output file would look like this:
TTL
Longest line (14153): EcclesiastesWho can fathom the soundless depths?two men out of all
Average length: 58
Twenty Thousand Leagues Under the Seas
An Underwater Tour of the World
JULES VERNE
[......]
If there are multiple lines with the same length as the longest, use the line number to decide: the line that appears later is considered as "longest". Round the average to the closest integer.
And thats it! Youve gathered useful information, unscrambled the text, and written the information to a new file.

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 Programming Questions!