저는 Python을 처음 사용합니다. 이 부분을 함수 전체에서 공유되는 변수로 만들고 싶습니다.Python : 함수간에 변수 공유
publist = []
publication = {"pubid" : 1, "title" : 2, "year" : 3, "pubtype" : 4, "pubkey" :5}
article = False
book = False
inproceeding = False
incollection = False
pubidCounter = 0
어디서 변수를 배치합니까? 아래에 나와있는 것처럼 배치하려고 시도했지만 indentation에 오류가 있다고합니다. 그러나 외부에 배치하면 들여 쓰기 오류가 발생합니다. 당신이 클래스 로컬로 정의가처럼 그들을 배치 할 때
import xml.sax
class ABContentHandler(xml.sax.ContentHandler):
publist = []
publication = {"pubid" : 1, "title" : 2, "year" : 3, "pubtype" : 4, "pubkey" :5}
article = False
book = False
inproceeding = False
incollection = False
pubidCounter = 0
def __init__(self):
xml.sax.ContentHandler.__init__(self)
def startElement(self, name, attrs):
if name == "incollection":
incollection = true
publication["pubkey"] = attrs.getValue("pubkey")
pubidCounter += 1
if(name == "title" and incollection):
publication["pubtype"] = "incollection"
def endElement(self, name):
if name == "incollection":
publication["pubid"] = pubidCounter
publist.add(publication)
incollection = False
#def characters(self, content):
def main(sourceFileName):
source = open(sourceFileName)
xml.sax.parse(source, ABContentHandler())
if __name__ == "__main__":
main("dblp.xml")