如何解决Python遍历列表中的页面和项目,但输出顺序不正确
我的代码遍历PDF的每一页,然后为我提供所有页面的关键字。我希望代码一旦找到就停止迭代该页面,然后再次从页面1开始搜索第二个关键字,当发现第二个关键字时停止并继续下一个关键字。
请有人帮我
示例输入文字
第1页: 今天天气很好。目前,系统是如此多样化。可持续性很重要。系统存在于公司中。
第2页: 我们对此不确定。生物特征数据在那里。技术是最好的。技术很重要。
第3页: 今天是星期一。银首饰存在。理想的数据库已有多年历史了。银很好。
以下代码:
import fitz #(Python PyMuPDF library)
import pandas as pd
keywords= ['systems','biometric','technology','silver','puppies']
filename = r"myfile"
doc =fitz.open(filename)
page=doc[0]
lst=[]
#open page in PDF
for page in doc:
text = page.getText("text")
data = ''.join(text)
data=str(data)
# add a full stop where there are short sentence
for line in data.split('\n'):
if 4 <= len(line) <= 20:
line=line+'.'
#iterate through keywords list
for item in keywords:
#if present then print
if item in line:
lst.append((line.split('.')))
print('\nKEYWORD:{} \n OUTPUT \n'.format(item),line,page number)
break
#else if not found in whole document then print not found
else:
lst.append('Not found')
print('not found')
当前输出:
systems are so varied currently 1
systems are present in the company 1
biometric data is there 2
technology is the best 2
technology is important 2
silver jewellery is present 3
silver is nice 3
所需的输出
systems are so varied currently 1
biometric data is there 2
technology is the best 2
silver jewellery is present 3
puppies: 'not found '
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。