-->

类型错误:__init __()恰恰1个参数(3中给出)的PyXML(TypeError: __in

2019-06-26 22:41发布

我最近开始学习如何使用Python来解析XML文件。 我把教程从http://pyxml.sourceforge.net/topics/howto/node12.html

当我运行下面的代码我得到的错误:

Traceback (most recent call last):
  File "C:\Users\Name\Desktop\pythonxml\tutorials\pythonxml\pyxml sourceforge\5.1 Comic Colection\SearchForComic.py", line 30, in -toplevel-
    dh = FindIssue('sandman', '62')
TypeError: __init__() takes exactly 1 argument (3 given)

码:

from xml.sax import saxutils

class FindIssue(saxutils.DefaultHandler):
    def __init___(self, title, number):
        self.search_title, self.search_number = title, number

def startElement(self, name, attrs):
    #if it's not a comic element, ignore it
    if name!= 'comic': return

        # look for the title and number sttributes (see text)
        title = attrs.get('title', None)
        number = attrs.get('number', None)
        if (title == self.search_title and
            number == self.search_number):
                print title, '#' +str (number), 'found'

from xml.sax import make_parser
from xml.sax.handler import feature_namespaces

if __name__ == '__main__':
        #Create a parser
        parser = make_parser()

    #tell the parser that we are not interested in XML namespaces
        parser.setFeature(feature_namespaces, 0)

    #create the handler
    dh = FindIssue('sandman', '62')

    #tell the parse to use our handler
    parser.setContentHandler(dh)

    #parse the input
    parser.parse('collection.xml')

还就上线,我传递文件及其在当前工作目录,这是解决文件的正确方法是什么?

Answer 1:

你有太多的_在__init__的名称。 你的构造函数的声明应该是:

def __init__(self, title, number):

不:

def __init___(self, title, number):


Answer 2:

你有一个错字 - 有3下划线的位置:

def __init___(self, title, number):

应该:

def __init__(self, title, number):

因为它不完全名称匹配__init__ ,巨蟒只知道默认的构造函数, def __init__(self)



文章来源: TypeError: __init__() takes exactly 1 argument (3 given) pyXML