W3docs

UnicodeDecodeError, invalid continuation byte

Here is an example of a Python code snippet that could cause a UnicodeDecodeError: invalid continuation byte error:

Here is an example of a Python code snippet that could cause a UnicodeDecodeError: invalid continuation byte error:

producing UnicodeDecodeError in Python

python— editable, runs on the server
Info

Watch a video course: Python - The Practical Guide

In this example, the invalid_text variable contains a byte sequence that is not valid UTF-8 encoded text. The decode_text function attempts to decode this text using the UTF-8 encoding, but the decoding process raises a UnicodeDecodeError because the byte 0x80 is not a valid continuation byte in the UTF-8 encoding. The error message is then caught and handled by the except block, which returns the string "Invalid continuation byte" instead.