UnicodeEncodeError
charmap codec
encoding error
Python error
character encoding

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:

python
1text = "Status: café, 東京, and 👍"
2
3with open("report.txt", "w", encoding="cp1252") as f:
4    f.write(text)

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.

python
1text = "Status: café, 東京, and 👍"
2
3with open("report.txt", "w", encoding="utf-8") as f:
4    f.write(text)

The same rule applies when reading files:

python
1with open("report.txt", "r", encoding="utf-8") as f:
2    content = f.read()
3
4print(content)

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:

python
import sys

print(sys.stdout.encoding)

If the terminal is not using UTF-8, printing certain characters may fail. In Python 3.7 and later, you can reconfigure the stream:

python
1import sys
2
3sys.stdout.reconfigure(encoding="utf-8")
4print("Ready: café, 東京, 👍")

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.

python
1with open("legacy-input.txt", "r", encoding="cp1252") as f:
2    text = f.read()
3
4normalized = text.replace("–", "-")
5
6with open("normalized.txt", "w", encoding="utf-8") as f:
7    f.write(normalized)

If you cannot avoid bad characters, Python also supports error handlers:

python
1text = "café 東京 👍"
2
3encoded = text.encode("cp1252", errors="replace")
4print(encoded)

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.
  • 'charmap usually points to a legacy locale-based encoding such as cp1252.'
  • Explicitly use encoding="utf-8" for modern file I/O whenever possible.
  • Console output can fail too, so inspect or reconfigure sys.stdout when needed.
  • Avoid masking the problem with lossy error handlers unless data loss is acceptable.

Course illustration
Course illustration

All Rights Reserved.