Created
February 25, 2017 04:17
-
-
Save K-Wu/cd9ca275767bfa30718cad0466f9df25 to your computer and use it in GitHub Desktop.
import to quizlet from bing dictionary
This file contains 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
#!/usr/bin/python2.7 | |
# -*- coding: UTF-8 -*- | |
import sys | |
if len(sys.argv)!=3: | |
print "Usage:" | |
print "python bing2quizlet.py BingDictExport.txt QuizletImport.txt" | |
exit(1) | |
print sys.argv | |
infile=sys.argv[1] | |
outfile=sys.argv[2] | |
word_anchor=u"单词:" | |
anchor=u":" | |
word_dict={} | |
encoding_string="utf8" | |
with open(infile) as ifd: | |
line=ifd.readline() | |
try: | |
line = unicode(line, "utf8") | |
except UnicodeDecodeError: | |
print "the encoding of input file is not utf8, try gb2312" | |
encoding_string="gb2312" | |
with open(infile) as ifd: | |
lines=ifd.readlines() | |
for idx in range(len(lines)): | |
line=unicode(lines[idx],encoding_string) | |
if line.find(word_anchor)==0: | |
word_start_index=line.find(anchor)+1 | |
word=line[word_start_index:-1] | |
idx+=2 | |
line=unicode(lines[idx],encoding_string) | |
meaning_start_index=line.find(anchor)+1 | |
meaning=line[meaning_start_index:] | |
word_dict[word]=meaning | |
with open(outfile,'w') as ofd: | |
for word,meaning in word_dict.items(): | |
line=word+u"\t"+meaning | |
line=line.encode("utf8") | |
ofd.write(line) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment