簡體   English   中英

我的IF語句出現語法錯誤,不確定為什么嗎?

[英]I'm getting a syntax error on my IF statement, not sure why?

我正在嘗試在python 3.7中運行以下代碼。 我不斷收到無效的語法錯誤,不確定為什么,有人可以發現我在做什么錯嗎? 縮進似乎很好,我相信我的“打印件”放在正確的括號中,但我完全不了解“ if”和“ else”語句。

class pdfPositionHandling:

    def parse_obj(self, lt_objs):

        # loop over the object list
        for obj in lt_objs:

            if isinstance(obj, pdfminer.layout.LTTextLine):
                print ("%6d, %6d, %s" % (obj.bbox[0], obj.bbox[1], obj.get_text().replace('\n', '_'))

            # if it's a textbox, also recurse
            if isinstance(obj, pdfminer.layout.LTTextBoxHorizontal):
                self.parse_obj(obj._objs)

            # if it's a container, recurse
            elif isinstance(obj, pdfminer.layout.LTFigure):
                self.parse_obj(obj._objs)

    def parsepdf(self, filename, startpage, endpage):

        # Open a PDF file.
        fp = open(filename, 'rb')

        # Create a PDF parser object associated with the file object.
        parser = PDFParser(fp)

        # Create a PDF document object that stores the document structure.
        # Password for initialization as 2nd parameter
        document = PDFDocument(parser)

        # Check if the document allows text extraction. If not, abort.
        if not document.is_extractable:
            raise PDFTextExtractionNotAllowed

        # Create a PDF resource manager object that stores shared resources.
        rsrcmgr = PDFResourceManager()

        # Create a PDF device object.
        device = PDFDevice(rsrcmgr)

        # BEGIN LAYOUT ANALYSIS
        # Set parameters for analysis.
        laparams = LAParams()

        # Create a PDF page aggregator object.
        device = PDFPageAggregator(rsrcmgr, laparams=laparams)

            # Create a PDF interpreter object.
        interpreter = PDFPageInterpreter(rsrcmgr, device)


        i = 0
        # loop over all pages in the document
        for page in PDFPage.create_pages(document):
            if i >= startpage and i <= endpage:
                # read the page into a layout object
                interpreter.process_page(page)
                layout = device.get_result()

                # extract text from this object
                self.parse_obj(layout._objs)
            i += 1

我收到以下錯誤:

File "C:/Users/951298/Documents/Python Scripts/PDF Scraping/untitled1.py", line 12
    if isinstance(obj, pdfminer.layout.LTTextBoxHorizontal):
                                                           ^
SyntaxError: invalid syntax

不確定為什么它最后指向結腸?

在第9行中,您應該在最后鍵入3個括號,但您只能輸入2個括號,再添加一個括號即可。

您忘記在打印對帳單上放置結尾括號。 這會導致下一行錯誤,因為解釋器在讀取方括號內的代碼時會忽略換行符。 實際上,它在第12行引發錯誤的唯一原因是, if isinstance(obj, pdfminer.layout.LTTextBoxHorizontal):不是傳遞給print的有效參數。

因此,以下代碼將在第11行上引發錯誤。

bar = "a"
baz = "a"

def foo(msg, bar="\n"):
    print(msg, end=bar)

if bar == baz:
    foo("bar is equal to baz",
    bar = baz

else: #Throws error here
    foo("bar is not equal to baz")

#Not the best example, I know, sorry.

奇怪,不是嗎? 確保查看拋出錯誤的行上方的行。 它為您提供了上下文和潛在的錯誤代碼。 您特別需要注意需要換行符終止的編程語言中的此類錯誤。

在第9行中,您應該有3個結尾括號,但是我也偶然注意到您有兩個if語句和一個elif語句,但沒有其他,它們都應該是if語句。 希望我能幫上忙!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM