试图开发一些可以自动将文件中的内容翻译成不同语言但只希望在标记区域之间翻译的东西。问题是:我可以指定 .read 只在让我们说“”之间读取吗?
在我的文件中,我有一个句子或单词的列表,甚至可以说字母,它们就在那里。我在句子或要翻译的单词周围加上“”语音标记。
下面是我的txt文件:
Sentence 1 - "I like Bannannas and I will eat them all day."
Sentence 2 - How is your day going?
Sentence 3 - "Will there be any sort of fun today or just raining?"
Sentence 4 - Can the sun come out to play!!!
我希望能够翻译现在只包裹在“”周围的句子。
我的代码目前:
import re
import googletrans
from googletrans import Translator
file_translator = Translator()
tFile = open('demo.txt', 'r', encoding="utf-8")
if tFile.mode == 'r':
content = tFile.read()
print(content)
result = file_translator.translate(content, dest='fr')
with open('output.txt', 'w') as outFile:
outFile.write(result.text)