当前位置:天才代写 > tutorial > Python教程 > python 有道词典导出的xml生词本文件提取出单词

python 有道词典导出的xml生词本文件提取出单词

2018-05-17 08:00 星期四 所属: Python教程 浏览:422

python">
import xml.sax
import os,sys

class WordsHandler(xml.sax.ContentHandler):
    def __init__(self):
        self.CurrantTag = ''
        self.Word = ''
        self.Trans = ''
        self.wordfile = open('words.txt', 'w')

    def startElement(self, tag, attributes):
        self.CurrantTag = tag

    def endElement(self, tag):
        if self.CurrantTag == 'word':
            self.wordfile.write(self.Word.encode("utf-8") + '\n')
            print "Word : ", self.Word
        elif self.CurrantTag == 'trans':
            print "Trans : ", self.Trans
        self.CurrantTag = ''

    def characters(self, contant):
        if self.CurrantTag == 'word':
            self.Word = contant
        elif self.CurrantTag == 'trans':
            self.Trans = contant

    def endDocument(self):
        self.wordfile.close()

if __name__ == '__main__':
    parser = xml.sax.make_parser()
    parser.setFeature(xml.sax.handler.feature_namespaces, 0)

    handler = WordsHandler()
    parser.setContentHandler(handler)

    parser.parse("words.xml")

标签:python

 

    关键字:

天才代写-代写联系方式