我是python的新手。我想使这部分变量在函数中共享。Python:跨功能共享变量
publist = []
publication = {"pubid" : 1, "title" : 2, "year" : 3, "pubtype" : 4, "pubkey" :5}
article = False
book = False
inproceeding = False
incollection = False
pubidCounter = 0
我在哪里放置这些变量。我已经尝试将它放置如下所示,但它表示有一个indendation错误。但是,将它们放在外面也会导致缩进错误。
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")