使用Microsoft翻译服务将英语翻译成中文

时间:2018-11-13 11:11:27

标签: python python-3.x python-requests microsoft-translator

我正在尝试使用Microsoft翻译服务(azure)将一些英语文本翻译成中文,并请求python库。

问题是我收到翻译时输入的字符错误。

我已经检查了响应编码,它是utf-8,所以应该可以。

我将以下代码用于翻译:

url = 'https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&from=en-US&to=zh-CN&textType=html"

headers = {
    'Ocp-Apim-Subscription-Key': secretKey,
    'Content-type': 'application/json',
    'X-ClientTraceId': str(uuid.uuid4())
}

body = [{
    'text': string
}]

response = requests.post(url, headers=headers, json=body)
print(response.encoding) # This prints utf-8
response = request.json()
print(response[0]['translations'][0]['text'] # prints garbage
return response

这里会发生什么?

1 个答案:

答案 0 :(得分:0)

您获得的“垃圾”是中文,但仍被编码为UTF-8,因此必须将其视为原始字节字符串并进行解码。

使用示例结果str

str = b'\xe4\xbd\xa0\xe5\xa5\xbd\xe5\x90\x97\xef\xbc\x9f'
print (str.decode('utf8'))

收益

你好吗?

发音为“ Ni hao ma?”并且中文是“你好吗?”