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

def decode_text(text):
    try:
        return text.decode('utf-8')
    except UnicodeDecodeError:
        return "Invalid continuation byte"

invalid_text = b'\x80abc'
decoded_text = decode_text(invalid_text)
print(decoded_text)

<div class="alert alert-info flex not-prose"> Watch a course <span class="hidden md:block">Watch a video course </span> Python - The Practical Guide</div>

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.