想在有道词典生词本中导入大量单词比较不方便,所以我写了一python脚本,用来将txt文件格式的英文单词存入有道词典生词本,方便复习。
首先要求txt文档里单词的格式,大概需要像这种:
每行是一个单词,格式为:英文单词+空格+词性+单词意思。
python代码为:
import os
import logging
def change_words(sentence, name):
sentence_depart = sentence.split(' ', 1)
words = sentence_depart[0]
trans = sentence_depart[1]
words_line = "<item> <word>{}</word>\r<trans> " \
"<![CDATA[{}]]></trans>\r<tags>{}</tags>" \
"\r</item>\r".format(words, trans, name)
return words_line
fileDir = "./"
fileFormat = ".txt"
input_file = "none.txt"
ouput_file = "none.xml"
head = "<wordbook>"
file_name = ''
logging.basicConfig(level=logging.DEBUG)
for root, dirs, files in os.walk(fileDir):
for item in files:
if item.find(fileFormat) != -1:
input_file = item
file_name = item.split('.')[0]
ouput_file = file_name + ".xml"
logging.info(ouput_file)
break
inputs = open(input_file, 'r', encoding='utf-8')
outputs = open(ouput_file, 'w', encoding='utf-8')
for item in inputs:
head += change_words(item, file_name)
head += "</wordbook>"
outputs.write(head)
outputs.close()
inputs.close()
脚本运行之后,会在脚本目录下生成.xml文件即为可以导入有道生词本的文件,大概长这样:
然后将xml文件导入有道词典生词本就行了,最后应该是这样:
最后我打包了一下,用的时候只需要将txt文件和可执行文件放一个文件夹就行了,需要用的的可以拿(附赠考研单词),网盘链接在最后:
链接: https://pan.baidu.com/s/1AqVL-KC8imLL1QvwQ_qZYg 提取码: hhcx