Ashish Bansal
Ashish Bansal

Reputation: 111

Save Amazon Translate output to .txt file using Boto3

I am trying to use the below query to fetch the translate the data using AWS Translate API, however I am unable to find the suitable way of saving the result in .txt format.

import boto3

data = 'file.txt'

translate = boto3.client(service_name='translate', region_name='us-east-1', use_ssl=True)

with open('file.txt', 'r') as text:data = text.read() 

result = translate.translate_text(Text=data, 
        SourceLanguageCode="en", TargetLanguageCode="de")

print('TranslatedText: ' + result.get('TranslatedText'))
print('SourceLanguageCode: ' + result.get('SourceLanguageCode'))
print('TargetLanguageCode: ' + result.get('TargetLanguageCode'))

output = out_fun()
file = open("sample.txt","w")
file.write(output)
file.close()

I am expecting to save the results as per C:\Users\Ashish\Documents\Script

Upvotes: 1

Views: 798

Answers (1)

John Rotenstein
John Rotenstein

Reputation: 269520

How about:

file = open("sample.txt","w")
file.write(result.get('TranslatedText'))
file.close()

Upvotes: 2

Related Questions