Python - 试图在单独的 py 文件中导入类

标签 python python-3.x

我已经使用 python 工作了大约 2 周,我已经能够导入访问数据库并存储来自 sql select 的值,但是其中一个字段中嵌入了 html 标记。我正在尝试使用 html_parser,我在一个单独的 py 文件中创建了类 def,我将其导入到我的主文件中。当我尝试从我的主文件调用例程时,出现错误。这是我的主 py 文件中的命令

from html.parser import HTMLParser
import html_parser
parser = MyHTMLParser()

这是 html_parser.py 文件中的命令

 class MyHTMLParser(HTMLParser):
 def handle_starttag(self, tag, attrs):
      print("Encountered a start tag:", tag)

 def handle_endtag(self, tag):
      print("Encountered an end tag :", tag)

 def handle_data(self, data):
      print("Encountered some data  :", data)
      global data_str
      data_str = data_str + "!#@@@@@#!" + data

 global data_str
 data_str = ""

这是当我使用主 py 文件从命令提示符运行 python 时出现的错误

C:\Users\Owner\AppData\Local\Programs\Python\Python39>python py_script8.py 导入 MyHTMLParser 类 追溯(最近一次通话): 文件“C:\Users\Owner\AppData\Local\Programs\Python\Python39\py_script8.py”,第 36 行,位于 解析器 = MyHTMLParser() NameError: name 'MyHTMLParser' is not definedBlockquote

如果有人有任何见解,将不胜感激。 (这是与 python 一起工作的乐趣。)

**** 解决方案 ***** MM真的帮了我!非常感谢!这是我所做的-

靠近主 py 文件的顶部,已将其添加以运行 html_parser。

import html_parser

if __name__ == '__main__':

在一个从 for 循环运行的函数中,循环遍历存储在从导入的访问数据库中获取所有行的 sql 语句中的记录

 global r_str
 parser.data_str = ""
        
 parser.feed(r_str)
 #print(parser.data_str)
 r_str = parser.data_str

html_parser.py 的内容是这样的:

from html.parser import HTMLParser
 
class MyHTMLParser(HTMLParser):

    def __init__(self):
        # Superclass initialization.
        super().__init__()
        # Variables are initialized here.
        self.data_str = ""

    def handle_starttag(self, tag, attrs):
        print("Encountered a start tag:", tag)

    def handle_endtag(self, tag):
        print("Encountered an end tag :", tag)

    def handle_data(self, data):
        print("Encountered some data  :", data)
        self.data_str += "!#@@@@@#!" + data
    

最佳答案

更正了答案。

请试试这个。
主要代码写在task.py中。

任务.py

import html_parser

if __name__ == '__main__':
    # The initialization of the class of the external file(module name+.py) is "module name.classname()".
    parser = html_parser.MyHTMLParser()
    # Insert the HTML here.
    parser.feed('<html><head><title>Parser Test</title></head>'
            '<body><BLOCKQUOTE>Quoted content</BLOCKQUOTE></body></html>')
    # In this way, you can retrieve the contents stored in the parser class.
    print(parser.data_str)

html_parser.py

from html.parser import HTMLParser
 
class MyHTMLParser(HTMLParser):

    def __init__(self):
        # Superclass initialization.
        super().__init__()
        # Variables are initialized here.
        self.data_str = ""

    def handle_starttag(self, tag, attrs):
        print("Encountered a start tag:", tag)

    def handle_endtag(self, tag):
        print("Encountered an end tag :", tag)

    def handle_data(self, data):
        print("Encountered some data  :", data)
        self.data_str += "!#@@@@@#!" + data

预期输出如下。

Encountered a start tag: html
Encountered a start tag: head
Encountered a start tag: title
Encountered some data  : Parser Test
Encountered an end tag : title
Encountered an end tag : head
Encountered a start tag: body
Encountered a start tag: blockquote
Encountered some data  : Quoted content
Encountered an end tag : blockquote
Encountered an end tag : body
Encountered an end tag : html
!#@@@@@#!Parser Test!#@@@@@#!Quoted content

关于Python - 试图在单独的 py 文件中导入类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66961661/

相关文章:

python - 从另一个类中的 InputText 获取值。基维

python - 类实例化在 python 中给出位置错误

python - 使用次要术语(决胜局)在 python 中排序计数器集合

python - 如何添加 try & except 构造以便脚本忽略不存在文件

python - 谷歌的速率限制是多少

python - 如何在python中设置时间戳

python - 似乎无法打印任何内容

Python:使用几乎相同的代码 try catch 多个异常

python - 无法在 docker 镜像中导入 cx_Oracle

python - 如何在 with 语句中有多个虚拟文件编写器?