TypeError:__init __()恰好接受1个参数(给定3个)pyXML [英] TypeError: __init__() takes exactly 1 argument (3 given) pyXML

查看:95
本文介绍了TypeError:__init __()恰好接受1个参数(给定3个)pyXML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

运行以下代码时出现错误:

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)

代码:

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?

推荐答案

您在__init__的名称中有太多_.您的构造函数的声明应为:

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

def __init__(self, title, number):

不是:

def __init___(self, title, number):

这篇关于TypeError:__init __()恰好接受1个参数(给定3个)pyXML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆