-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordParse.py
65 lines (57 loc) · 1.75 KB
/
wordParse.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import xml.sax
import sys
import time
class WordHandler(xml.sax.ContentHandler):
def __init__(self):
self.CurrentData = ''
self.word = 'abc'
self.phonetic = ''
self.trans = ''
def startElement(self, tag, attributes):
self.CurrentData = tag
def endElement(self, tag):
# str = self.word + self.phonetic + self.trans + '\n'
# f.write(str)
# print(self.word, self.phonetic, self.trans)
if self.CurrentData == 'word':
# print(self.word, end = '')
pass
elif self.CurrentData == 'phonetic':
# print(self.phonetic, end = '')
pass
elif self.CurrentData == 'trans':
# print(self.trans)
pass
if tag == 'item':
a = self.word.ljust(20)
b = self.phonetic.ljust(20)
c = self.trans.replace('\n', ' ').ljust(60)
# str = self.word + self.phonetic + self.trans + '\n'
str = a + b + c + '\n'
# print(str)
f.write(str)
self.phonetic = ''
self.trans = ''
self.CurrentData = ""
def characters(self, content):
# print(content)
# print(self.CurrentData)
if self.CurrentData == 'word':
self.word = content
elif self.CurrentData == 'phonetic':
self.phonetic = self.phonetic + content
elif self.CurrentData == 'trans':
self.trans = self.trans + content
# print(self.trans)
if __name__ == '__main__':
global f
f = open("youdao.txt", 'r+', encoding='utf-8')
parser = xml.sax.make_parser()
handler = WordHandler()
# parser.setContentHandler(handler)
# ret = parser.parse("C:\\Users\\10251\\Documents\\YouDaoDic.xml")
# parser.parse('C:\\Users\\10251\\Documents\\YouDaoDic.xml', handler)
xml.sax.parse('C:\\Users\\10251\\Documents\\YouDaoDic.xml', handler)
# with open("youdao.txt", 'r+') as f:
# f.write("abc")
f.close()