2012-08-10 2 views
4

최근에 파이썬을 사용하여 xml 파일을 구문 분석하는 방법을 배우기 시작했습니다. 내가 오류를 다음 코드를 얻을 실행할 때 나는 http://pyxml.sourceforge.net/topics/howto/node12.htmlTypeError : __init __()은 정확히 하나의 인수 만받습니다. 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') 

또한 마지막 줄에 내가 전달 해요 파일을 현재 작업 디렉토리에 저장하는 것이 올바른 방법입니까?

답변

8

__init__의 이름에 _이 너무 많습니다. 생성자의 선언은 다음과 같아야합니다

def __init__(self, title, number): 

하지 :

def __init___(self, title, number): 
+0

남자 나는 믿을 수 없다는 오류 메시지가 –

+4

인 것을 나는 알지 못했다. izkata는'__init__'이 존재하지 않는다고 지적했기 때문에, 파이썬은 스스로를 인수로 취하는 기본 생성자로 돌아가므로 파이썬은 적절한 수를 가진 것으로 보이는 함수에 너무 많은 인수를 사용한다고 불평합니다. –

4

당신은 오타가 - 3 거기가 여기에 밑줄 :

def __init___(self, title, number): 

은 다음과 같아야합니다

def __init__(self, title, number): 

정확히 matc가 아니기 때문에 h __init__이라는 이름을 사용하면 파이썬은 기본 생성자 def __init__(self)에 대해서만 알고 있습니다.

관련 문제