UnicodeEncodeError 'charmap' codec can't encode characters
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
UnicodeEncodeError: 'charmap' codec can't encode characters means Python tried to turn a Unicode string into bytes using an encoding that cannot represent one or more characters. The failure is common on Windows consoles, legacy text files, and any environment where the default encoding is not UTF-8.
Why This Error Happens
In Python 3, strings are Unicode. The error appears only when Python needs to encode that Unicode text into bytes, such as when writing to a file, printing to a terminal, or saving CSV output.
The word charmap usually points to a platform-specific single-byte encoding such as cp1252. That encoding can represent many Western characters, but it cannot represent every Unicode symbol. If your text contains characters such as emoji, CJK text, or some punctuation marks, the encoder raises an exception instead of guessing.
This simple example reproduces the problem:
That fails because cp1252 cannot encode every character in the string.
Fix It by Choosing the Right Encoding
The best fix is usually to use UTF-8 explicitly anywhere text crosses a file or process boundary. UTF-8 can encode all Unicode characters and is the safest default for modern applications.
The same rule applies when reading files:
Being explicit avoids accidental dependence on the machine's locale and makes the program behave consistently across environments.
Printing to the Console
Sometimes the error comes from print, not from file I/O. On some systems, standard output still uses a narrow encoding. You can inspect the active console encoding like this:
If the terminal is not using UTF-8, printing certain characters may fail. In Python 3.7 and later, you can reconfigure the stream:
That is useful in scripts, but for production systems it is even better to run the program in a UTF-8 environment when possible.
Handling External Data Sources
Sometimes you do not control the source encoding. A file from another system may truly be cp1252, latin-1, or something else. In that case, decode using the correct source encoding, then work with Unicode internally, and finally encode to UTF-8 for output.
If you cannot avoid bad characters, Python also supports error handlers:
errors="replace" prevents the crash, but it loses information. Use it only when character loss is acceptable.
Common Pitfalls
The first pitfall is relying on the system default encoding. Code that works on one laptop may fail on another machine with a different locale.
Another common mistake is mixing text and bytes carelessly. If you decode with one encoding and later encode with another without thinking about the data, corruption or exceptions follow quickly.
Developers also use errors="ignore" to silence the error. That makes the program continue, but it silently drops characters and can damage user data or logs.
A final pitfall is assuming the terminal and files share the same encoding. You might write a UTF-8 file successfully but still fail when printing the same string to a console using a legacy code page.
Summary
- The error happens when Python tries to encode Unicode text using an encoding that cannot represent all characters.
- '
charmapusually points to a legacy locale-based encoding such ascp1252.' - Explicitly use
encoding="utf-8"for modern file I/O whenever possible. - Console output can fail too, so inspect or reconfigure
sys.stdoutwhen needed. - Avoid masking the problem with lossy error handlers unless data loss is acceptable.

