简体   繁体   中英

TypeError: itertuples() missing 1 required positional argument: 'self'

I am trying to read PDF file and save some data

import PyPDF2
import os
from pandas import DataFrame as df

FilePath = "/home/milenko/Documents/komp/"
FileName = "stats.pdf"
output_filename = "export.txt"
PageStart = 184
PageEnd = 185

for row in df.itertuples():
    page_start, page_end = row.PageStart, row.PageEnd
    filename = os.path.join(row.FilePath, row.FileName)
    with PdfFileMerger() as merger:
        merger.append(filename, pages=(page_start, page_end))
        merger.write(output_filename)

I got this

  File "b21.py", line 11, in <module>
    for row in df.itertuples():
TypeError: itertuples() missing 1 required positional argument: 'self'

My original effort was to read one page from terminal but I got data that were not formatted at all

 pageObj = pdfReader.getPage(184)
"Appendix tables171Table A5 (continued) World merchandise exports by region and selected economy, 1990-00(Million dollars)19901991199219931994199519961997199819992000  Slovenia --668160836828831683128372904886048733  Spain556426017764334I609357312991613102091104277109037110246113747  Sweden5754055217561184985761292795848487982883849448484286933  Switzerland6378461517656786318570360816418084376150788568030081534 

How to solve this problem?I am looking for any solution that will print data into tables or whatever that can latter be exported and manipulated.

About the TypeError :

itertuples is a Dataframe method, ie it has to be called from an instance of the class Dataframe . You're renaming the class Dataframe to df when you do from pandas import DataFrame as df , instead of instancing it.

Make an instance with df = Dataframe() and then you'll be able to call itertuples() (obviously you also need to fill the dataframe with data before being able to iterate over it).

About your use of itertuples :

I'm not completely sure of what you're trying to do here, but I think you should take a look at the itertuples documentation (and the example within) . The function iterates over the data inside the dataframe . You're not even opening your pdf file, let alone reading data from it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM