Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 扫描文本文档中的字符串并保存OCCCURNCE索引列表—;奇怪的图案_Python_List_Indexing_Text_Iteration - Fatal编程技术网

Python 扫描文本文档中的字符串并保存OCCCURNCE索引列表—;奇怪的图案

Python 扫描文本文档中的字符串并保存OCCCURNCE索引列表—;奇怪的图案,python,list,indexing,text,iteration,Python,List,Indexing,Text,Iteration,Python3.8-我有一个pdf文档,它是多个3到5页的文档,所有这些文档串在一起形成一个pdf。我把它转换成txt文件就行了 我需要从每个文档中提取一些信息,因此我将文本文档拆分为几行,并尝试简单地扫描这些行,如果行中显示“第1页”,则将该索引存储到列表中。然后我将知道如何在找到第1页的前两个索引之间搜索我的第一个文档的信息 这个索引值列表的结果让我很困惑。请看下面 pdf_list = next(os.walk(pdf_directory))[2] for each_pdf in pdf

Python3.8-我有一个pdf文档,它是多个3到5页的文档,所有这些文档串在一起形成一个pdf。我把它转换成txt文件就行了

我需要从每个文档中提取一些信息,因此我将文本文档拆分为几行,并尝试简单地扫描这些行,如果行中显示“第1页”,则将该索引存储到列表中。然后我将知道如何在找到第1页的前两个索引之间搜索我的第一个文档的信息

这个索引值列表的结果让我很困惑。请看下面

pdf_list = next(os.walk(pdf_directory))[2]

for each_pdf in pdf_list:
    extension = str(each_pdf[-4:])
    if extension == '.pdf':
        with open(each_pdf, "rb") as f:
            pdf = pdftotext.PDF(f)
        with open(str(each_pdf[:-4] + '.txt'), "w") as f:
            f.write("\n\n".join(pdf))
#All groovy so far.. turned it into a .txt file. Looks correct.

for each_txt in pdf_list:
    extension = str(each_txt[-4:])
    if extension == '.txt':
        with open(each_txt, "r") as f:
            text = f.read()
            lines = text.splitlines()
            page_1_indices = []

            for each in lines:
                if "Page 1" in each:
                    new_index = lines.index(each)
                    page_1_indices.append(new_index)
            print(page_1_indices)
这是输出。。。它如此不稳定地循环回到以前的点,以至于我看不到模式。我是说。。。就在大门外,它在索引4上再次命中221


[0, 88, 221, 310, 221, 874, 986, 221, 1303, 0, 221, 986, 1915, 874, 2155, 986, 2512, 2155, 986, 3079, 310, 3336, 2155, 3674, 1915, 2155, 4181, 4434, 2155, 4743, 4743, 986, 5235, 2155, 2155, 1915, 2155, 0, 2155, 0, 2155, 221, 5235, 4743, 2155, 2155, 221, 986, 2155, 2155, 8259, 4743, 0, 0, 8728, 4181, 221, 8259, 2155, 221, 2155, 5235, 0, 986, 10691, 1915, 2155, 3674, 0, 8259, 986, 986, 221, 12362, 0, 986, 0, 5235, 5235, 13232, 5235, 0, 5235, 986, 2155, 1915, 1915, 1915, 2155, 14646, 221, 221, 221, 2155, 1915, 3079, 221, 0, 221, 2155, 2155, 5235, 0, 1915, 2155, 2155, 2155, 2155, 2155, 2155, 2155, 2155, 2155, 2155, 2155, 2155, 2155, 2155, 2155, 2155, 2155, 2155, 874, 874, 2155, 2155, 2155, 20468、20468、20468、20468、20468、20468、20468、20468、20468、20468、20468、22193、20468、20468、22193、20468、20468、20468、22193、20468、20468、20468、20468、20468、20468、20468、22193]
我想我已经成功了。我只是直接引用了文本文件,不再尝试以其他方式提取索引,而是使用了枚举

with open(txt_file, "r") as f:
    text = f.read()
    lines = text.splitlines()
    page_1_indices = []
    for (i, j) in enumerate(lines):
        if "Page 1" in j:
            page_1_indices.append(i)
    print(page_1_indices)
输出:


[0, 88, 221, 310, 523, 874, 986, 1167, 1303, 1392, 1476, 1691, 1915, 2062, 2155, 2295, 2512, 2719, 2868, 3079, 3214, 3336, 3517, 3674, 3838, 3985, 4181, 4434, 4612, 4743, 4886, 5044, 5235, 5385, 5584, 5759, 5887, 6034, 6121, 6249, 6327, 6470, 6562, 6833, 7016, 7164, 7364, 7696, 7917, 8110, 8259, 8399, 8526, 8639, 8728, 8862, 9088, 9346, 9504, 9638, 9871, 10049, 10385, 10473, 10691, 10891, 11028, 11209, 11408, 11488, 11647, 11934, 12179, 12362, 12541, 12627, 12830, 12918, 13075, 13232, 13304, 13489, 13578, 13749, 13968, 14104, 14226, 14385, 14507, 14646, 14867, 15159, 15295, 15386, 15543, 15671, 15851, 16063, 16148, 16348, 16472, 16635, 16718, 16806, 16965, 17068, 17255, 17376, 17509, 17672, 17816, 18009, 18178, 18317, 18445, 18626, 18784, 18947, 19069, 19256, 19437, 19571, 19700, 19824, 19934, 20096, 20295, 20468, 20602, 20818, 21017, 21216, 21337, 21458, 21579, 21713, 21912, 22039, 22193, 22333, 22507, 22694, 22828, 22985, 23166, 23353, 23481, 23609, 23731, 23883]

您是否可以共享部分输入?很遗憾,由于文档中的个人信息,我无法共享。我将构建一个测试用例并运行它。