I need to read the non-english text from the text file line by line and translate to english and write that to another text file, also read english text from text file and translate that to non-english(could be any native language depends on requirement) and save that translated text to a new text file.
Imported googletrans3.1.0a0 to translate.
The reading and writing part fails when its non-english.
my code:
import googletrans
from googletrans import Translator
translator = Translator(service_urls=['translate.googleapis.com'])
with open('Tobetranslated.txt', 'r') as f:
with open('output.txt', 'w') as w:
f_contents = str(f.readline())
while len(f_contents) > 0:
print(f_contents, end="")
translated = translator.translate(f_contents, src='ko', dest='en')
print(translated.text)
w.write(str(translated.text) + "\n")
f_contents = f.readline()
The error: UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 4: character maps to