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 (将#修改为@)