Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ContentHandler未定义_Python_Xml_Sax_Saxparser - Fatal编程技术网

Python ContentHandler未定义

Python ContentHandler未定义,python,xml,sax,saxparser,Python,Xml,Sax,Saxparser,我正试图从O'Reilly学习Python的SAX模块。我正在尝试运行下面的示例代码,但我一直收到一个错误,我不知道为什么 第一个文件是handlers.py: class ArticleHandler(ContentHandler): """ A handler to deal with articles in XML """ def startElement(self, name, attrs): print "Start element:"

我正试图从O'Reilly学习Python的SAX模块。我正在尝试运行下面的示例代码,但我一直收到一个错误,我不知道为什么

第一个文件是
handlers.py

class ArticleHandler(ContentHandler):
    """
    A handler to deal with articles in XML
    """

    def startElement(self, name, attrs):
        print "Start element:", name
第二个文件是
art.py
,它导入第一个文件:

#!/usr/bin/env python
# art.py

import sys

from xml.sax import make_parser
from handlers import ArticleHandler

ch = ArticleHandler( )

saxparser = make_parser( )
saxparser.setContentHandler(ch)
saxparser.parse(sys.stdin)
当我尝试运行
art.py
时,我得到以下结果:

% python art.py < article.xml
Traceback (most recent call last):
  File "art.py", line 7, in <module>
    from handlers import ArticleHandler
  File "~/handlers.py", line 1, in <module>
    class ArticleHandler(ContentHandler):
NameError: name 'ContentHandler' is not defined
%python art.py
我可能错过了一些明显的东西。有人能帮忙吗


谢谢

您必须在handlers.py中导入ContentHandler,如下所示:

from xml.sax.handler import ContentHandler
这就可以了。

谢谢!成功了。(不知何故,我在书的勘误表中遗漏了这一点,但以下是:)