Last active
December 14, 2015 23:39
-
-
Save annidy/5166927 to your computer and use it in GitHub Desktop.
有道单词本转换为金山单词本
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf8 -*- | |
# author: [annidy](http://blog.csdn.net/ani_di) | |
# date: 2013.3.15 | |
# | |
# 有道单词本转换为金山单词本 | |
# | |
from __future__ import print_function | |
from xml.dom import minidom | |
import getopt, sys | |
def usage(): | |
print("Convert Youdao word to kingsoft word utility") | |
print(sys.argv[0], "[-i youdao.xml]", "[-o kingsoft.txt]") | |
print("pipe is support") | |
def main(): | |
try: | |
opts, args = getopt.getopt(sys.argv[1:], "hi:o:") | |
except getopt.GetoptError as err: | |
# print help information and exit: | |
print(err) | |
usage() | |
sys.exit(2) | |
ifp = sys.stdin | |
ofp = sys.stdout | |
for o, a in opts: | |
if o == "-i": | |
ifp = open(a, 'r') | |
elif o == "-o": | |
ofp = open(a, 'w') | |
elif o == "-h": | |
usage() | |
sys.exit() | |
else: | |
assert False, "unhandled option" | |
xmldoc = minidom.parseString(ifp.read()) | |
wordlist = xmldoc.getElementsByTagName('word') | |
translist = xmldoc.getElementsByTagName('trans') | |
phoneticlist = xmldoc.getElementsByTagName('phonetic') | |
for i in range(len(wordlist)): | |
try: | |
print('+', wordlist[i].firstChild.data, sep='', file=ofp) | |
for t in translist[i].firstChild.data.splitlines(): | |
print('#', t, sep='', file=ofp) | |
phonetic = phoneticlist[i].firstChild.data | |
print('&', phonetic[1:len(phonetic)-1], sep='', file=ofp) # remove [] | |
pass | |
except Exception, e: | |
print(e, file=sys.stderr) | |
finally: | |
print('$3', sep='', file=ofp) | |
pass | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment