Python遍历列表中的页面和项目,输出不按顺序

Python遍历列表中的页面和项目,输出不按顺序,python,python-3.x,pandas,list,pymupdf,Python,Python 3.x,Pandas,List,Pymupdf,我的代码遍历PDF的每一页,然后给出所有页面的关键字。我希望代码在找到后停止迭代页面,然后再次从第1页开始搜索第二个关键字,找到后停止,继续搜索下一个关键字 有人能帮我吗 示例输入文本 第1页: 今天天气很好。目前的制度是如此多样化。可持续性很重要。系统在公司中存在 第2页: 我们对此不确定。生物特征数据就在那里。技术是最好的。技术很重要 第3页: 今天是星期一。有银首饰。理想的数据库就是这么多年的历史。银色很好 代码如下: import fitz #(Python PyMuPDF libra

我的代码遍历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
biometric data is there 2
technology is the best 2
silver jewellery is present 3
puppies: 'not found '

您是否考虑过编写一个查找第一次出现的函数?它会返回,您可以循环到下一个搜索词。重新安排循环,使您的关键字位于第一位,程序会自上而下读取,以便在完成所有其他循环后进入下一个“页面”。如果有一个最小的工作示例,例如解析您可以提供的.txt文件,则会容易得多。在任何情况下,您确定要在另一个循环中使用
break
语句操作而不是使用
else
语句操作吗?@GhandiFloss嘿,您的意思是将关键字列表移到代码的中间吗?对不起,我不太清楚你的意思(新手程序员)@Maciek它不会让我上传一个虚拟的PDF文件,所以我举了一个输入文件的例子,并将输出与之关联。希望这能让事情变得更清楚。对于break语句,我认为它在找到关键字后结束循环,但它没有
systems are so varied currently 1
biometric data is there 2
technology is the best 2
silver jewellery is present 3
puppies: 'not found '