我正在尝试创建一个程序来搜索我在单独文档中的文章。我无法让我的程序搜索该词并允许我查看仅包含该搜索词的文档。理想情况下,我希望搜索输入类似于月亮,并允许我访问该文档。完整的文档如下所示,我的代码如下。
<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)