How to print out a value in a dictionary then values is a list?
Solution 1:
With the code below, I am able to get such output from your pdf file:
Output:
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 code:
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()
I'm not an expert in Python, but hope this will help you. By the way, you can read some info about recursions in Python here
Solution 2:
This line is not right:
if elements is {}: # If the element is a dictionary just find the Title
It should instead read:
if isinstance(elements, dict):