【问题标题】:How to print out a value in a dictionary then values is a list?如何在字典中打印出一个值然后值是一个列表?
【发布时间】:2022-01-23 13:29:57
【问题描述】:

编码领域:python3 中使用 pyPDF2 的 PDF 目录

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

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

这种模式重复多次。

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

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

计划:

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

非常感谢任何帮助。

【问题讨论】:

标签: python-3.x pypdf2


【解决方案1】:

通过下面的代码,我可以从你的 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

【讨论】:

【解决方案2】:

这行不对:

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

应该改为:

        if isinstance(elements, dict):

【讨论】:

  • 这帮助很大。但是我遇到了另一个问题,它只做#和#.#。我该如何进一步做我必须做的事情#.#.#。 ?
  • 你可能需要使用递归。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-07
  • 2022-11-24
  • 1970-01-01
  • 2019-05-03
相关资源
最近更新 更多