Python: Error translating text with api translate

0

I'm trying to do a translation with the api GoogleTrans ( link ). Code:

# -*- coding: utf-8 -*-
from googletrans import Translator
translator = Translator()

print translator.translate('hello', dest='pt')

I get the error:

UnicodeEncodeError: 'ascii' codec can't encode character u'\xe1' in position 35:
 ordinal not in range(128)

Can you help me with this question? I'm using PYTHON 2.7

    
asked by anonymous 23.04.2018 / 18:15

1 answer

2

You must specify that you expect a unicode string:

# -*- coding: utf-8 -*-
from googletrans import Translator
translator = Translator()

print unicode(translator.translate('hello', dest='pt'))
# Translated(src=en, dest=pt, text=Olá, pronunciation=None)

# O unicode não é necessário para mostrar só o texto
print translator.translate('hello', dest='pt').text
# Olá
    
23.04.2018 / 18:26