使用 python 向译者发送 utf-8 请求
Send utf-8 request to translator using python
我弄乱了一段代码。
我正在尝试使用 python 将不同语言的句子发送到 microsoft azure 翻译器,但我无法使 utf-8 工作。
我花了很多时间浏览互联网或聊天,所以既然我卡住了,我就在这里。
我正在使用 jupyter,所以这里是:
import requests
url="https://api.cognitive.microsofttranslator.com//detect?api-version=3.0"
params = (
('Subscription-Key', cle),
)
token = requests.post('https://northeurope.api.cognitive.microsoft.com/sts/v1.0/issueToken', params=params)
token.text
它用于为 Microsoft Azure 生成令牌,它工作正常。
import json
import codecs
paragraphe='シャーリー・フィールドは、サ'
paragraphe
我的句子,我导入 json 以备后用,当我尝试做 utf-8 时导入解码器
headers = {
'Authorization': 'Bearer ' + token.text,
'Ocp-Apim-Subscription-Region': 'northeurope',
'Content-Type': 'application/json',
}
data = '[{\'Text\':\'' + paragraphe + '\'}]'
#data = '[{\'Text\':\'\u30B7\u30E3\u30FC\u30EA\u30FC\u30FB\u30D5\u30A3\u30FC\u30EB\u30C9\u306F\u3001\u30B5\'}]'
#data = { 'Text' : paragraphe }
reponse = requests.post('https://api.cognitive.microsofttranslator.com//detect?api-version=3.0', headers=headers, params=params, data=data)
如果我使用“你好,你好吗?”这样的句子,这就是问题所在。它工作得很好,但是一旦我使用特殊字母,我就得到了:
UnicodeEncodeError: 'latin-1' codec can't encode characters in position 10-23: Body ('シャーリー・フィールドは、サ') is not valid Latin-1. Use body.encode('utf-8') if you want to send it encoded in UTF-8.
我认为这是一个简单的问题,但我需要一些帮助。
另外,如果我使用这样的东西:
data = json.dumps({'Text': "シャーリー・フィールドは、サ"}).encode('utf-8')
我明白了
'{"error":{"code":400074,"message":"The body of the request is not valid JSON."}}'
在reponse.text
非常感谢。
试试这个:
data=[{
"text":"シャーリー・フィールドは、サ"
}]
reponse = requests.post('https://api.cognitive.microsofttranslator.com/detect?api-version=3.0', headers=headers, data=str(data).encode('utf-8'))
print(reponse.text);
结果:
如果您需要更多帮助,请告诉我。
我弄乱了一段代码。
我正在尝试使用 python 将不同语言的句子发送到 microsoft azure 翻译器,但我无法使 utf-8 工作。
我花了很多时间浏览互联网或聊天,所以既然我卡住了,我就在这里。
我正在使用 jupyter,所以这里是:
import requests
url="https://api.cognitive.microsofttranslator.com//detect?api-version=3.0"
params = (
('Subscription-Key', cle),
)
token = requests.post('https://northeurope.api.cognitive.microsoft.com/sts/v1.0/issueToken', params=params)
token.text
它用于为 Microsoft Azure 生成令牌,它工作正常。
import json
import codecs
paragraphe='シャーリー・フィールドは、サ'
paragraphe
我的句子,我导入 json 以备后用,当我尝试做 utf-8 时导入解码器
headers = {
'Authorization': 'Bearer ' + token.text,
'Ocp-Apim-Subscription-Region': 'northeurope',
'Content-Type': 'application/json',
}
data = '[{\'Text\':\'' + paragraphe + '\'}]'
#data = '[{\'Text\':\'\u30B7\u30E3\u30FC\u30EA\u30FC\u30FB\u30D5\u30A3\u30FC\u30EB\u30C9\u306F\u3001\u30B5\'}]'
#data = { 'Text' : paragraphe }
reponse = requests.post('https://api.cognitive.microsofttranslator.com//detect?api-version=3.0', headers=headers, params=params, data=data)
如果我使用“你好,你好吗?”这样的句子,这就是问题所在。它工作得很好,但是一旦我使用特殊字母,我就得到了:
UnicodeEncodeError: 'latin-1' codec can't encode characters in position 10-23: Body ('シャーリー・フィールドは、サ') is not valid Latin-1. Use body.encode('utf-8') if you want to send it encoded in UTF-8.
我认为这是一个简单的问题,但我需要一些帮助。
另外,如果我使用这样的东西:
data = json.dumps({'Text': "シャーリー・フィールドは、サ"}).encode('utf-8')
我明白了
'{"error":{"code":400074,"message":"The body of the request is not valid JSON."}}'
在reponse.text
非常感谢。
试试这个:
data=[{
"text":"シャーリー・フィールドは、サ"
}]
reponse = requests.post('https://api.cognitive.microsofttranslator.com/detect?api-version=3.0', headers=headers, data=str(data).encode('utf-8'))
print(reponse.text);
结果:
如果您需要更多帮助,请告诉我。