UnicodeEncodeError: Character Cannot Be Encoded

UnicodeEncodeError: 'ascii' codec can't encode character

Quick Answer

You are encoding a Unicode string with an encoding that cannot represent all characters. Use UTF-8 encoding which supports all Unicode characters.

Why This Happens

Python 3 strings are Unicode by default, but when writing to files or stdout, they must be encoded. If the target encoding cannot represent certain characters, this error is raised.

The Problem

name = 'caf\u00e9'
with open('out.txt', 'w', encoding='ascii') as f:
    f.write(name)

The Fix

name = 'caf\u00e9'
with open('out.txt', 'w', encoding='utf-8') as f:
    f.write(name)

Step-by-Step Fix

  1. 1

    Use UTF-8 encoding

    Specify encoding='utf-8' when opening files.

  2. 2

    Set PYTHONIOENCODING

    Set PYTHONIOENCODING=utf-8 for stdout issues.

  3. 3

    Handle errors gracefully

    Use errors='replace' for unencodable characters.

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free