Python: Error translating text with the Translate api

Asked

Viewed 635 times

0

I’m trying to do a translation with the Googletrans api(https://pypi.org/project/googletrans/). 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

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á
  • Unicodeencode: 'charmap' codec can’t Encode characters in position 114-115: Character maps to <Undefined> And if this occurs?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.