I've messing with a piece of code.
I'm trying to send sentences in different languages to microsoft azure translator using python, but I can't make utf-8 work.
I spend many times look all over internet or asking into chat, so since i'm stuck, here I am.
I'm using jupyter so here it is :
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
It's use to generate the token for Microsoft Azure, it works properly.
import json
import codecs
paragraphe='シャーリー・フィールドは、サ'
paragraphe
My sentence, I import json for later, and codecs when i was trying to do 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)
This is where is goes wrong, if I use a sentence like "hello how are you ?" it works perfectly, but as soon as I use special letters, I got :
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.
I believe this is a simple problem, but I need some help.
Also if I use something like this :
data = json.dumps({'Text': "シャーリー・フィールドは、サ"}).encode('utf-8')
I get
'{"error":{"code":400074,"message":"The body of the request is not valid JSON."}}'
In reponse.text
Many thanks.
