如何在字典中打印出一个值然后值是一个列表?

How to print out a value in a dictionary then values is a list?

编码区域:使用 pyPDF2

python3 中的 PDF Table 内容

问题:我需要一个程序可以迭代包含多个字典的联合变量,然后是包含多个字典的多个列表。

[
    {},
    [{}, {}, {}],
    {},
    [{}, {}, {}],
    {},
    [{}, {}, {}]
]

此模式重复多次。

预期输出:输出应如下所示

1 Title Goes Here
   1.1 Title Goes Here
       1.1.1 Title Goes Here
       1.1.2 Title Goes Here
       1.1.3 Title Goes Here
   1.2 Title Goes Here
       1.2.1 Title Goes Here
       1.2.2 Title Goes Here
       1.2.3 Title Goes Here
   1.3 Title Goes Here
       1.3.1 Title Goes Here
       1.3.2 Title Goes Here
       1.3.3 Title Goes Here

2 Title Goes Here
   2.1 Title Goes Here
       2.1.1 Title Goes Here
       2.1.2 Title Goes Here
       2.1.3 Title Goes Here
   2.2 Title Goes Here
       2.2.1 Title Goes Here
       2.2.2 Title Goes Here
       2.2.3 Title Goes Here
   2.3 Title Goes Here
       2.3.1 Title Goes Here
       2.3.2 Title Goes Here
       2.3.3 Title Goes Here

程序:[=3​​1=]

import argparse as arp
from PyPDF2 import PdfFileReader

parser = arp.ArgumentParser()
parser.add_argument("-f", "--file", help="File to analyse")
arg = parser.parse_args()
filename = arg.file

def fileread():
    doc = PdfFileReader(filename)
    ToC = doc.getOutlines()

    # ToC: Union[List[Union[Destination, list]], {__eq__}] = doc.getOutlines()

    for elements in ToC:
        #print(elements)
        #print("\n")

        try:
            if elements is {}: # If the element is a dictionary just find the Title
                print(elements['/Title']) # TODO: This is just skipped 

            else: # If the element is a list go through and print out the titles
                for nest_dict in elements:
                    try:
                        print(nest_dict["/Title"])
                    except:
                        continue
        except:
            continue

fileread()

我正在测试此程序:Compilers - Principles, Techniques, and Tools-Pearson_Addison Wesley (2006).pdf

非常感谢任何帮助。

这行不对:

        if elements is {}: # If the element is a dictionary just find the Title

应该改为:

        if isinstance(elements, dict):

使用下面的代码,我可以从您的 pdf 文件中获得这样的输出:

输出:

1 Introduction
1.1 Language Processors
1.1.1 Exercises for Section 1.1
1.2 The Structure of a Compiler
...
2 A Simple Syntax-Directed Translator
2.1 Introduction
2.2 Syntax Definition
2.2.1 Definition of Grammars
...

Python代码:

import argparse as arp
from PyPDF2 import PdfFileReader

parser = arp.ArgumentParser()
parser.add_argument("-f", "--file", help="File to analyse")
arg = parser.parse_args()
filename = arg.file

def fileread():
    doc = PdfFileReader(filename)
    ToC = doc.getOutlines()

    for elements in ToC:
        try:
            def print_title(input_data):
               if isinstance(input_data, dict):
                    print(input_data['/Title'])
               else:
                    for nest_dict in input_data:
                        try:
                            print_title(nest_dict)
                        except:
                            continue
            print_title(elements)
        
        except:
            continue       
fileread()

我不是 Python 方面的专家,但希望这对您有所帮助。顺便说一下,您可以在 Python here

中阅读一些关于递归的信息