-
Notifications
You must be signed in to change notification settings - Fork 0
/
TranslateForAnki.py
71 lines (61 loc) · 2.44 KB
/
TranslateForAnki.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
66
67
68
69
70
71
import http.client
import hashlib
import json
import urllib
import random
import csv
import time
import os
def baidu_translate(content):
appid = '20151113000005349'
secretKey = 'osubCEzlGjzvw8qdQc41'
httpClient = None
myurl = '/api/trans/vip/translate'
q = content
fromLang = 'en' # 源语言
toLang = 'zh' # 翻译后的语言
salt = random.randint(32768, 65536)
sign = appid + q + str(salt) + secretKey
sign = hashlib.md5(sign.encode()).hexdigest()
myurl = myurl + '?appid=' + appid + '&q=' + urllib.parse.quote(
q) + '&from=' + fromLang + '&to=' + toLang + '&salt=' + str(
salt) + '&sign=' + sign
try:
httpClient = http.client.HTTPConnection('api.fanyi.baidu.com')
httpClient.request('GET', myurl)
# response是HTTPResponse对象
response = httpClient.getresponse()
jsonResponse = response.read().decode("utf-8") # 获得返回的结果,结果为json格式
js = json.loads(jsonResponse) # 将json格式的结果转换字典结构
# dst = str(js["trans_result"][0]["dst"]) # 取得翻译后的文本结果
dst = js["trans_result"][0]["dst"] # 取得翻译后的文本结果
# print(isinstance(dst,str),':',dst) # 打印结果
return dst
except Exception as e:
print(e)
finally:
if httpClient:
httpClient.close()
def run(infile, outfile):
pairs = []
with open(infile, 'r', encoding='utf-8') as fin:
for line in fin:
# print(line=='\n' or '='in line,'---->',line.strip())
if not (line == '\n' or '='in line):
en = line.strip()#.encode('gbk', 'ignore').decode('gbk')
# print(isinstance(en,str),':',en)
zh = baidu_translate(en)
time.sleep(1)
print(zh)
pairs.append([zh, en]) # 第一个字段是正面,第二个是背面
with open(outfile, 'w', newline='', encoding='utf8') as fout:
csv_writer = csv.writer(fout)
csv_writer.writerows(pairs)
if __name__ == '__main__':
default_path = 'input.txt'
default_csv = r'output.csv'
run(infile=default_path, outfile=default_csv)
time = time.strftime(r'%y-%m-%d',time.localtime())
newname = '文献好句子_'+time+'.txt'
os.rename(default_path, newname)
print('原文件已经更名为:', newname)