0

I'm trying to create a string with special characters from portuguese-brazil (e.g. á, à, â, ç)

push_message = 'á'
push_message.decode().encode('utf-8')

But I'm getting this error:

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

I need to send this to Parse Api, and Parse Api just allows utf8 encoding. What can I do to solve this?

EDIT

When I try

push_message.decode('utf-8')

I get

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

When I try

push_message.decode('latin-1')

I get

UnicodeEncodeError: 'ascii' codec can't encode character u'\xe1' in position 0: ordinal not in range(128)
9
  • So what codec are you decoding from? Using the default, ASCII, obviously doesn't work, but you haven't told us anything about what codec your data is encoded in.
    – Martijn Pieters
    Commented Mar 28, 2015 at 14:16
  • 2
    If you are trying to create a Unicode literal, you need to use u'á' instead, then encode that. You'll have to provide a PEP 263 codec header in your file if you are to use non-ASCII characters in your source code however.
    – Martijn Pieters
    Commented Mar 28, 2015 at 14:17
  • 1
    You probably want to read the Python Unicode HOWTO here.
    – Martijn Pieters
    Commented Mar 28, 2015 at 14:18
  • @PadraicCunningham thanks for the help. I tried and edited my question. Can you check? Commented Mar 28, 2015 at 14:23
  • 1
    @PadraicCunningham got it! Commented Mar 28, 2015 at 15:10

1 Answer 1

0

It worked like this:

unicode(push_message.decode('utf-8')) 
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.