python 如何实现文档翻译
本帖最后由 fa00x 于 2019-7-9 15:36 编辑from translate import Translator
def tr(sm):
# 以下是将简单句子从英语翻译中文
translator= Translator(to_lang="chinese")
translation = translator.translate(sm)
print (translation + '\n')
sm= ''
tr(sm)
有一个文档 g:\2.txt
想把2.txt内容通过上面代码给翻译 出来 结果保存。
感谢@额微粒波地
提示最大翻译500个单词。
from translate import Translator
def tr(sm):
# 以下是将简单句子从英语翻译中文
translator= Translator(to_lang="chinese")
translation = translator.translate(sm)
#filename = 'fanyi.txt'
print(translation)
f = open("./22.txt",encoding= 'utf-8')
sm= f.read()
tr(sm) 嗯,你看看
额微粒波地 发表于 2019-7-9 15:08
嗯,你看看
感谢,最近学习python各种问题。谢谢 #!/usr/bin/env python3
# -*- coding: utf-8 -*-
#! author = ixsec
from translate import Translator
def main():
files = open("2.txt",encoding="utf-8")
files2 = open("2tozh.txt",'a+',encoding="utf-8")
text_lines = files.readlines()
print("---------------------------翻译开始-----------------------------")
for i in text_lines:
translator = Translator(to_lang="zh")
translation = translator.translate(i)
files2.write(translation+"\n")
print(translation)
files.close()
files2.close()
print("---------------------------翻译结束-----------------------------")
if __name__ == "__main__":
main() 我也截个图~~
页:
[1]