0
試圖編寫一個程序,該程序將查看包含NYTimes暢銷書籍的文本文件,並讓用戶從列表中搜索並打印圖書。從具有參數的文本文件打印Python
目前有這樣的代碼,打印包含他們鍵入書的名稱行:
def lookup():
file= input("Please enter the text file path: ")
search = input("Enter the books you wish to search for: ")
search = [word.strip() for word in search.lower().split(",")]
with open(file, 'r') as f:
for line in f:
line = "\n" + line
for word in search:
if word.lower() in line.lower():
print(line)
lookup()
我將如何讓一年範圍內用戶的搜索? txt文件包含書名,作者,發表年份等,全部由1個製表符分隔。
搜索範圍有點棘手,但是FWIW,您可以使用[csv](https://docs.python.org/3/library/csv.html)模塊以更方便的方式讀取數據。我建議將你的文件讀入詞典列表,每本書一本詞典。 –