To fix a python UnicodeDecodeError, find out which encoding the bytes really use and decode them with that encoding. To fix a UnicodeEncodeError, make sure the target encoding can represent every character, usually by writing UTF-8. Both errors mean that bytes and text were converted with the wrong assumption, so the right fix is to be explicit about the encoding at every boundary, not to hide the error.
The examples use Python 3.13 and 3.14, but the behaviour is the same in every Python 3 release.
Python 3 separates text (str) from bytes. Converting between them needs an encoding, and two exceptions describe the failures:
| Error | Direction | Typical cause |
|---|---|---|
UnicodeDecodeError | bytes to str | The bytes are not valid in the codec you chose |
UnicodeEncodeError | str to bytes | The codec cannot represent a character in the string |
Both are subclasses of UnicodeError, which is a subclass of ValueError. The message tells you the codec, the position and the offending byte or character.
The most common cause is opening a file without an encoding. open() then uses the locale encoding, which on some systems is not UTF-8, so a UTF-8 file fails or a legacy file is misread.
If the file is not UTF-8, work out what it is:
cp1252 or utf-8-sig.EF BB BF has a byte order mark, so use utf-8-sig.0xe9 or 0x92.charset-normalizer. It returns a guess, not a guarantee.Be careful with latin-1: it maps every byte to a character, so it never raises an error, but it will produce mojibake (wrong characters) if the file was really UTF-8.
When bytes arrive from an HTTP response, a socket or a subprocess, decode them with the encoding the protocol states, not a guess.
Libraries such as requests and httpx give you response.text using the charset from the headers. If the header is missing or wrong, set it yourself or decode response.content. Never call str(some_bytes): it returns the b'...' representation instead of decoding and hides the problem.
An encode error appears when the output codec is narrower than the text, for example ASCII, or a legacy Windows console codec.
For printing to a terminal or a redirected pipe, the stream encoding comes from the environment. Fixes, from best to worst:
python -X utf8 script.py or set PYTHONUTF8=1.PYTHONIOENCODING=utf-8 for the standard streams.sys.stdout.reconfigure(encoding="utf-8").Only choose a lossy fix when the receiver truly cannot take UTF-8, and then do it on purpose.
The errors argument of encode(), decode() and open() controls what happens on bad data.
Use replace for logs and display, backslashreplace for diagnostics, and xmlcharrefreplace when producing ASCII-only HTML or XML. ignore silently deletes data, so avoid it for anything you care about. surrogateescape lets you read bytes that are not valid text and write them back unchanged, which is how Python handles odd file names on Linux.
When you do not know where the bad bytes come from, inspect them instead of guessing.
The exception has start, end, object, encoding and reason attributes. Printing the bytes around the failure usually shows whether you are seeing Latin-1 or cp1252 text, a truncated multi-byte sequence, or binary data (an image or gzip file) that was never text.
The environment picked it, often a minimal container with no locale set, or a pipe. Pass encoding="utf-8" explicitly, or set PYTHONUTF8=1.
It makes the exception disappear by deleting the bad bytes, so data can be lost without any warning. Use it only when losing those characters is acceptable.
The decoder met a byte that cannot begin a UTF-8 sequence, such as 0xe9 or 0x92. It nearly always means the data is in a single-byte encoding like Latin-1 or cp1252.
A multi-byte character was cut off, for example by reading a fixed number of bytes in the middle of it. Read the whole stream or use an incremental decoder.
Fix Unicode errors by naming the encoding at every boundary: pass encoding to open(), decode network bytes with the declared charset, and write UTF-8. Inspect the failing bytes to identify the real source encoding, and pick an error handler deliberately rather than reaching for ignore.