1

我正在尝试使用以下查询来使用 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

4

1 回答 1

2

怎么样:

file = open("sample.txt","w")
file.write(result.get('TranslatedText'))
file.close()
于 2019-06-12T07:28:10.357 回答