python isinstance in loop nested

35
December 11, 2019, at 00:50 AM

What is the most efficient way for python to drill down a nested list of 'PyPDF2.generic.Destination' and list objects to get to the last instance of a list? (to get to all levels of a PDF outline and its page number).

pdfread = p2.PdfFileReader(list_files)
s = pdfread.outlines
chapters = []
pages = []
levels = []
files = []
for x in s:
    files.extend([x])
    if isinstance(x, (int, list, float, complex)):
        for y in x:
            if isinstance(y, (int, list, float, complex)):
                for z in y:
                    if isinstance(z, (int, list, float, complex)): 
                        for a in z:
                            if isinstance(a, (int, list, float, complex)): 
                                for c in a:
                                    if isinstance(c, (int, list, float, complex)): 
                                        print(c.title)
                                    else:
                                        chapters.extend([c.title])
                                        pages.extend([pdfread.getDestinationPageNumber(c)])
                                        levels.extend(['5'])
                            else:
                                chapters.extend([a.title])
                                pages.extend([pdfread.getDestinationPageNumber(a)])
                                levels.extend(['4'])
                    else:
                        chapters.extend([z.title])
                        levels.extend(['3'])
                        pages.extend([pdfread.getDestinationPageNumber(z)])
            else:
                chapters.extend([y.title])
                levels.extend(['2'])
                pages.extend([pdfread.getDestinationPageNumber(y)])
    else:
        chapters.extend([x.title])
        levels.extend(['1'])
        pages.extend([pdfread.getDestinationPageNumber(x)])
df = pd.DataFrame({'chapters': chapters, 'pages' : pages, 'levels' : levels})
df.head(44)
Answer 1

Just define a recursive function:

def nested_is_instance_check(s, level, pages):
    level +=1
    if isinstance(x, (int, list, float, complex)):
        for x in s:
            nested_is_instance_check(s, level, pages)
    else:
        chapters.extend([y.title])
        levels.extend(['level'])
        pages.extend([pdfread.getDestinationPageNumber(s)])

nested_is_instance_check calls itself for every level in your file.

READ ALSO
How to run a python script with another virtual environment dependencies in a DAG in Airflow?

How to run a python script with another virtual environment dependencies in a DAG in Airflow?

How to run a python script with another virtual environment dependencies in a DAG in Airflow? Not using the main python environment where airflow is installed

46
Error when opening executable in raspberry pi [closed]

Error when opening executable in raspberry pi [closed]

Want to improve this question? Update the question so it's on-topic for Stack Overflow

66
Getting error about argument when calling method

Getting error about argument when calling method

I have feature file from where I am trying to get the email :

38
How to save results of lenghty calculation with multiple parameters?

How to save results of lenghty calculation with multiple parameters?

I am doing some lengthy calculations with multiple parametersI would like to save and retrieve the results of these calculations

53