python批量翻译各种备注信息

(1)小强版

import http.client
import hashlib
import urllib
import random
import json

def etoc(text):
    appid = ''  # 填写你的appid
    secretKey = ''  # 填写你的密钥

    myurl = '/api/trans/vip/translate'

    fromLang = 'auto'  # 原文语种
    toLang = 'zh'  # 译文语种
    salt = random.randint(32768, 65536)
    q = text
    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()
        result_all = response.read().decode("utf-8")
        result = json.loads(result_all)

        return  result["trans_result"][0]["dst"]

    except Exception as e:
        print(e)
        return  ""

# print(etoc("china")) #单元测试

with open("inflection.py","r") as f,open("D:\\md\\b.txt","w") as fw:
    for line in f.readlines():
        fw.write(line.lstrip()+'\n')
file = open("D:\\md\\b.txt","rb")
newfile = open("inflection_cn.py","wb")
newfile.write("##########\r\n".encode("utg-8"))
newfile.write("#机器学习-微信:qxskying\r\n".encode("utg-8"))
newfile.write("#QQ:2375184137\r\n".encode("utg-8"))
newfile.write("##########\r\n".encode("utg-8"))
# print(file.read())
while True:
    line = file.readline()
    if not line:
        break
    line=line.decode("utf-8")
    # line.lstrip()
    if line[0] == "#":
        print(line,end="")
        print("#"+etoc(line[1:])+"\r\n",end="")
        newfile.write(("#"+etoc(line[1:])+"\r\n").encode("utf-8"))
    else:
        print(line,end="")
        newfile.write(line.encode("utf-8"))
file.close()
newfile.close()

备注:

 appid = ''  # 填写你的appid
 secretKey = ''  # 填写你的密钥

需要到百度ai上获取,或者直接在被人放出来的类似文件里获得

 

文件地址自己选择地方放,文件格式也一样

 

 

(2)加强版,pdf版

略。

 

(3)试用版,可以面向各种格式的版本

略。

 

(4)商用版,混用数据库,前端,后台等的,工具版本。

略。

 

(5)进化版本,可以盈利,可对接版本。

略。

 

(6)中级进化版本

略。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值