我最近開始學習如何使用python來解析xml文件。 我從http://pyxml.sourceforge.net/topics/howto/node12.htmlTypeError:__init __()只需要1個參數(給出3個參數)pyXML
教程當我運行下面的代碼我得到的錯誤:
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')
還就上線,我在路過在當前工作目錄中存儲它是否是解決該文件的正確方法?
男人我不能相信我沒有注意到 – 2012-08-10 16:05:08
它的一種混亂的錯誤。正如izkata指出,如果'__init__'不存在,python會回退到默認構造函數,它只會將自身作爲參數,所以python會抱怨太多的參數,因爲您看到的函數具有正確的編號。 – 2012-08-10 16:11:00