我有一个包含以下内容的文本文件:
<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
我正在尝试创建一个python脚本,可以将其拆分为多个&#34;文档&#34;然后搜索它们,如果我搜索月亮这个词,它应该告诉我它在第2和第3个文件中找到了。在寻求帮助之后,我设法打印出它找到的单词,但它没有打印出它所在的文档(我试图实现它但是它与我的关系并不好。)
非常感谢任何帮助!
到目前为止,这是代码:
def main():
docs=[]
with open("ap_docs2.txt") as f:
lines = f.read().split("<NEW DOCUMENT>")[1:]
for x in lines:
docs.append(x.strip())
numOfDocs=len(docs)
docs1 = map(str.lower, docs) ## Convert list to lower case for search
print (docs)
print("Number of Documents:",numOfDocs)
search = input("Enter search words: ")
for x in docs1:
if search in x:
print ("{} found in:\t {}".format(search,x))
main()
答案 0 :(得分:1)
这会做你想要的吗?我只是在文档中打印匹配行的索引。
def main():
docs=[]
with open("ap_docs2.txt") as f:
lines = f.read().split("<NEW DOCUMENT>")[1:]
for x in lines:
docs.append(x.strip().lower())
numOfDocs=len(docs)
print (docs)
print("Number of Documents:",numOfDocs)
search = input("Enter search words: ")
for x in docs:
if search in x:
print('Found in document %d' % (docs.index(x) + 1))
main()