1

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

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470

1 Answers1

2

How about:

file = open("sample.txt","w")
file.write(result.get('TranslatedText'))
file.close()
John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
  • Thanks John, it worked like a charm. But is there a way of not selecting the any particular file and let it pick all the files present in folder and then convert them one by one. – Ashish Bansal Jun 12 '19 at 07:57
  • You can use os.listdir() method in python to list all the files in folder and then loop through the names. – Ninad Gaikwad Jun 12 '19 at 08:49
  • @AshishBansal The API for Amazon Translate accepts text as input and returns text as output. Anything you wish to do with files is your responsibility. – John Rotenstein Jun 12 '19 at 10:15
  • @JohnRotenstein But there is a limitation of data that you can input, so you have to break it into small streams and then provide these inputs. – Ashish Bansal Jun 12 '19 at 10:21
  • @NinadGaikwad Yes..saw your reply on another thread, I will check it and confirm it to you. – Ashish Bansal Jun 12 '19 at 10:24