+ -
当前位置:首页 → 问答吧 → 这几行python代码。执行有错?

这几行python代码。执行有错?

时间:2011-12-27

来源:互联网

Python code
from xml.sax.handler import ContentHandler
from xml.sax import parse

class PageMaker(ContentHandler):
    passthrough = False
    def startElement(self, name, attrs):
        if name == 'page':
            self.passthrough = True
            self.out = open(attrs[name]+'.html', 'w')
            self.out.write('<html><head>\n')
            self.out.write('<title>%s</title>\n' % attrs['title'])
            self.out.write('</head><body>\n')
        elif self.passthrough:
            self.out.write('<' + name)
            for key, val in attrs.items():
                self.out.write('"%s = %s"' %(key, val))
                self.out.write('>')


    def endElement(self, name):
                if name == 'page':
                    self.passthrough = False
                    self.out.write('\n</body></html>\n')
                    self.out.close()
                elif self.passthrough:
                    self.out.write('</%s>' % name)
    def characters(self, chars):
        if self.passthrough: self.out.write(chars)

parse('website.xml', PageMaker())

作者: cndeer   发布时间: 2011-12-27

没啥大错

作者: runer   发布时间: 2011-12-27