TypeError:__ init __()只需1个参数(给定3个)pyXML

时间:2022-07-07 23:17:06

I've recently started to learn how to use python to parse xml files. I took the tutorial from http://pyxml.sourceforge.net/topics/howto/node12.html

我最近开始学习如何使用python来解析xml文件。我从http://pyxml.sourceforge.net/topics/howto/node12.html获取了教程

When I run the following code I get the error:

当我运行以下代码时,我收到错误:

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)

code:

码:

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')

also on the last line I'm passing the file its in the current working directory is this the correct way to address the file?

也在最后一行我传递文件在当前工作目录中这是解决文件的正确方法吗?

2 个解决方案

#1


8  

You've got too many _ in the name of __init__. The declaration of your constructor should be:

你__init__的名字太多_了。构造函数的声明应该是:

def __init__(self, title, number):

not:

不:

def __init___(self, title, number):

#2


4  

You have a typo - there's 3 underscores here:

你有一个错字 - 这里有3个下划线:

def __init___(self, title, number):

Should be:

应该:

def __init__(self, title, number):

Because it doesn't exactly match the name __init__, Python only knows about the default constructor, def __init__(self).

因为它与名称__init__不完全匹配,所以Python只知道默认构造函数def __init __(self)。

#1


8  

You've got too many _ in the name of __init__. The declaration of your constructor should be:

你__init__的名字太多_了。构造函数的声明应该是:

def __init__(self, title, number):

not:

不:

def __init___(self, title, number):

#2


4  

You have a typo - there's 3 underscores here:

你有一个错字 - 这里有3个下划线:

def __init___(self, title, number):

Should be:

应该:

def __init__(self, title, number):

Because it doesn't exactly match the name __init__, Python only knows about the default constructor, def __init__(self).

因为它与名称__init__不完全匹配,所以Python只知道默认构造函数def __init __(self)。