1

I have this code

att=att.replace("à","a")

but I received this error...

  att=att.replace("à","a")
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in range(128)

How can I fix?

1
  • 1
    You can decode them. att=att.decode('utf-8').replace("à".decode('utf-8'),"a") Commented Aug 17, 2015 at 17:25

1 Answer 1

3

Don't mix unicode with byte strings. Python2 does implicit conversion, but Python3 does not. Even if you are not using Python3, it is good practice to avoid mixing the two.

In Python2, if att is a unicode, then

att.replace("à","a")

will implicitly try to decode "à" and "a"to be unicode as well before attempting to replace "à" in att. Python2 uses ascii (by default) to do the implicit decoding.

"à".decode('ascii') raises UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in range(128).

To fix this error, since att is unicode, att.replace's arguments should be unicode as well:

att.replace(u"à",u"a")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.