-1
我加載庫工作得很好,我的報告的作者在Python計算工作,但是當我把它上傳到一個自動評價它說如何修復我的報告功能?
=ERROR: File report.txt contains 0 lines, but shall contain 1 lines.
def load_library(f):
with open(f,'rt') as x:
return dict(map(str.strip, line.split("|")) for line in x)
def count_authors(file_name):
invert = {}
for k, v in load_library(file_name).items():
invert[v] = invert.get(v, 0) + 1
return invert
def write_authors_counts(counts, file_name):
with open(file_name, 'w') as fobj:
for name, count in counts.items():
fobj.write('{}: {}\n'.format(name, count))
def report_author_counts(lib_fpath, rep_filepath):
counts = count_authors(lib_fpath)
write_authors_counts(counts, rep_filepath)
報告作者計數
In module library.py, create function report_author_counts(lib_fpath, rep_filepath) which shall compute the number of books of each author and the total number of books, and shall store this information in another text file.
Inputs:
Path to a library text file (containing records for individual books).
Path to report text file that shall be created by this function.
Outputs: None
Assuming the file books.txt has the same contents as above, running the function like this:
>>> report_author_counts('books.txt', 'report.txt')
shall create a new text file report.txt with the following contents:
Clarke, Arthur C.: 2
Herbert, Frank: 2
Capek, Karel: 1
Asimov, Isaac: 3
TOTAL BOOKS: 8
The order of the lines is irrelevant. Do not forget the TOTAL BOOKS line! If the input file is empty, the output file shall contain just the line TOTAL BOOKS: 0.
Suggestion: There are basically 2 ways how to implement this function. You can either
use the 2 above functions to load the library, transform it using index_by_author() and then easilly iterate over the dictionary, or
you can work directly with the source text file, extract the author names, and count their occurences.
Both options are possible, provided the function will accept the specified arguments and will produce the right file contents. The choice is up to you.
一個問題,一個report.txt輸出需要有一個「總書:#」行。我沒有看到你的代碼。這有可能是在欺騙它嗎? –
是的,就是這就是爲什麼它不工作...... @羅布戴維斯固定下面 – jack