0
我正在嘗試創建一個程序來搜索我在單獨文檔中的文章。我無法讓我的程序搜索該術語並允許我查看僅包含搜索術語的文檔。理想情況下,我希望搜索輸入像月亮一樣,並允許我訪問該文檔。完整的文檔看起來像這樣,我的代碼如下。從python中的外部文件搜索和檢索文件
<NEW DOCUMENT>
Look on the bright
side of Life.
<NEW DOCUMENT>
look on the very, dark
side of the Moon
<NEW DOCUMENT>
is there life
on the moon
search = input("Enter search words: ")
docs = []
document = []
doc_search = []
for line in file2:
line = line.strip()
if line == "<NEW DOCUMENT>":
# start a new document
document = []
docs.append(document)
else:
# append to the current one
document.append(line)
docs = ['\n'.join(document) for document in docs]
for line in docs:
if line == search:
doc_search = []
doc_search.append(docs)