【python】使用腾讯翻译API对文件进行翻译

综合考虑腾讯翻译是目前翻译软件中免费字符数最多的一家厂商,一个月有500w免费字符

详细计费请查看以下链接

机器翻译 计费概述-购买指南-文档中心-腾讯云 (tencent.com)

申请

参考文档【萌译】如何注册腾讯云API_腾讯翻译api申请-CSDN博客

使用

python库下载及安装

tencentcloud-sdk-python · PyPI

https://download.csdn.net/download/qq_39397927/89876455

安装

https://blog.csdn.net/HUANG370/article/details/119182313

pip install tencentcloud_sdk_python-3.0.1247-py2.py3-none-any.whl

代码

读取文件可以换成pandas库

import csv
import io
import pandas as pd
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.tmt.v20180321 import tmt_client, models
SecretId = "**"
SecretKey = "**"
class Translator:
    def __init__(self, from_lang, to_lang):
        self.from_lang = from_lang
        self.to_lang = to_lang

    def translate(self, text):
        try:
            cred = credential.Credential(SecretId, SecretKey)
            httpProfile = HttpProfile()
            httpProfile.endpoint = "tmt.tencentcloudapi.com"

            clientProfile = ClientProfile()
            clientProfile.httpProfile = httpProfile
            client = tmt_client.TmtClient(cred, "ap-beijing", clientProfile)

            req = models.TextTranslateRequest()
            req.SourceText = text
            req.Source = self.from_lang
            req.Target = self.to_lang
            req.ProjectId = 0

            resp = client.TextTranslate(req)
            return resp.TargetText

        except TencentCloudSDKException as err:
            return err
#需要翻译的文件路径
translateFileSourcePath=r"**.csv"
#翻译后的文档路径
translateFileTargetPath=r"C:\Users\admin\Desktop\翻译.csv"
def get_csv(table):

    with io.open(table,'r', encoding='UTF-8', errors='ignore') as f:
        reader=csv.reader(f)
        return [row for row in reader]
if __name__ =="__main__":
    translator = Translator(from_lang="en", to_lang="zh")
    #存放翻译文档
    translatefile = {"content":[],"translate":[]}
    #读取CSV文件
    Table_fullmatch=get_csv(translateFileSourcePath)
    for item in Table_fullmatch:
        translatefile["content"].append(item[0])
        #进行翻译
        translateString = translator.translate(item[0])
        translatefile["translate"].append(translateString)
        print("翻译:"+translateString)
    df = pd.DataFrame(translatefile)
    df = df.drop_duplicates(subset=["content"])
    df.to_csv(translateFileTargetPath)
print("全部结束")

其他免费翻译

仅供参考

探索Python的翻译神器:Googletrans的神秘面纱_python googletrans-CSDN博客

Argos-Translate,一款超酷的python库 - 知乎 (zhihu.com)

Argos Translate 安装和配置指南-CSDN博客

参考文档

python实现调用腾讯云翻译API_腾讯翻译api怎么获取-CSDN博客

机器翻译 计费概述-购买指南-文档中心-腾讯云 (tencent.com)

免费翻译接口最新最全汇总(百度翻译,腾讯翻译,谷歌翻译,DeepL翻译等...)_免费翻译api-CSDN博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Z_W_H_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值