使用Boto3将Amazon Translate输出保存为.txt文件

时间:2019-06-12 07:07:02

标签: python amazon-web-services boto3 amazon-translate

我正在尝试使用以下查询来使用AWS Translate API来获取转换后的数据,但是我找不到合适的方法以.txt格式保存结果。

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()

我希望按照C:\Users\Ashish\Documents\Script

保存结果

1 个答案:

答案 0 :(得分:2)

怎么样:

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