简体   繁体   中英

Python string encode

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?

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM