使用堆栈来平衡html Python 3

时间:2019-09-27 13:47:22

标签: python html stack

我有一个作业,我应该在Python中使用堆栈检查外部html文件中是否有平衡标签(打开器与关闭器匹配)。我知道这里还有其他类似的问题,但是这些问题并没有多大帮助。我的想法是从“ <”和“>”中识别出的html标记中切出一部分,然后将标记名称仅压入堆栈。然后弹出结束标记,并将其与开始标记匹配,如果它们一直匹配,则认为html是平衡的。到目前为止,进展并不顺利。任何帮助将不胜感激。

class Stack:
    def __init__(self):
        self.items = []

    def isEmpty(self):
        return self.items == []

    def push(self, item):
        self.items.append(item)

    def pop(self):
        return self.items.pop()

    def peek(self):
        return self.items[len(self.items)-1]

    def size(self):
        return len(self.items)

def htmlChecker():
    f = open("nocloser.txt", "r")
    s = Stack()
    balanced = True

    for line in f:
        index = 0

    while index < len(line) and balanced:
        symbol = line[index]

        if symbol == "<":
            tempIndex = index + 1
            tagName = ""

            while line[tempIndex] != ">" and tempIndex < len(line):

                if line[tempIndex] in "<":
                    print("Invalid Syntax:", line.strip())

                tagName += line[tempIndex]
                tempIndex += 1

            if tagName[0] != "/":
                s.push(tagName)
                print("Opened tag with name:", tagName)
            else:
                if s.isEmpty() == False:
                    popped = s.pop()
                    print("Revmoving next line in stack", popped)
                else:
                    balanced = False
                    print("Closing tag without opener.", tagName)
            print("Stack", s)

        index += 1

    if balanced and s.isEmpty():
        return True
    else:
        return False

html文件:

<html>
    <head>
        <title>
            Example
        </title>
    </head>

    <body>
        <h1>Hello, world</h1>

</html>

再一次,我们将不胜感激。我想了解我要去哪里。

0 个答案:

没有答案