Avoiding UnicodeDecodeError

UnicodeDecodeError occurs whenever byte strings cannot decode to Unicode code points. To avoid this exception, we can pass replace, backslashreplace, or ignore to the error argument in decode the as shown here:

>>> str = b"xaf"
>>> str.decode('utf-8', 'strict')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xaf in position 0: invalid start byte

>>> str.decode('utf-8', "replace")
'ufffd'
>>> str.decode('utf-8', "backslashreplace")
'\xaf'
>>> str.decode('utf-8', "ignore")
' '
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset