Create a gist now

Instantly share code, notes, and snippets.

Embed
Prepare exported YouDao(有道) file for being imported to Anki
#!/usr/bin/python
import sys
import codecs
import subprocess
import os
print sys.argv
subprocess.call('iconv -f UTF-16 -t UTF-8 {} > {}.tmp'.format(sys.argv[1], sys.argv[1]), shell=True)
subprocess.call('dos2unix {}.tmp'.format(sys.argv[1]), shell=True)
output = codecs.open(sys.argv[1]+'.txt', 'w', encoding='utf-8')
t = ''
with codecs.open(sys.argv[1]+'.tmp', 'r', encoding='utf-8') as f:
for line in f:
num=line.split(u',', 1)
if num[0].isdigit() == True and line.startswith(num[0]):
if t:
output.write(t+u'\r\n');
t = num[1].replace(u'\n',u'\t')
else:
t += line.replace(u'\n',u' ')
output.write(t+u'\r\n');
output.close()
os.remove('{}.tmp'.format(sys.argv[1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment