批量翻译的小程序

一、先注册

http://ai.youdao.com/docs/doc-trans-price.s#p01

流程

http://ai.youdao.com/doc.s#guide

二、代码

#coding:UTF-8
import requests
import hashlib
import json
from collections import OrderedDict as Od
from file_handler import ReadFiles

import sys
reload(sys)
sys.setdefaultencoding('utf8')

salt = "**" (替换)
appKey = "****" (替换)
app_scret = "**" (替换)


def EnToChn(query_info,salt=salt,appKey=appKey,app_scret=app_scret):
    """
    将英文转化为中文
    """
    # query_info = "Acute lymphoblastic leukemia (B-cell precursor)"
    sign_info = appKey + query_info +salt + app_scret
    sign_info2 = hashlib.md5(sign_info).hexdigest()
    test_id = "http://openapi.youdao.com/api?q={query_info}&from=EN&to=zh_CHS&appKey={appKey}&salt={salt}&sign={sign_info2}" .format(**locals())
    r = requests.get(test_id)
    result_info = r.text
    result = json.loads(result_info)
    trans_result = result["translation"][0]
    return trans_result
个人公众号,比较懒,很少更新,可以在上面提问题:

更多精彩,请移步公众号阅读:

Sam avatar
About Sam
专注生物信息 专注转化医学