Fixing 'UnicodeEncodeError: ascii codec can't encode character' in Python

intermediate🐍 Python2026-03-27| This error can occur on any operating system (Windows, Linux, macOS) and any Python 3 version. It's particularly common when interacting with external resources like files, databases, or network streams that contain non-ASCII characters, and Python defaults to or is explicitly told to use the 'ascii' encoding.

Error Message

UnicodeEncodeError: 'ascii' codec can't encode character '\u00e9' in position 1: ordinal not in range(128)
#python#unicode#encoding#ascii#utf-8

Understanding the 'UnicodeEncodeError: ascii codec can't encode character'

You've hit a classic Python encoding snag if you're seeing an error like this:

UnicodeEncodeError: 'ascii' codec can't encode character '\u00e9' in position 1: ordinal not in range(128)

This error means your Python code is trying to convert a string that contains characters outside the standard ASCII range (0-127) into a byte sequence using the ascii codec. For instance, the character '\u00e9' is 'Ă©' (e-acute), which is common in many European languages but not part of the basic 128 ASCII characters.

Python 3 treats strings as Unicode by default, which is great. However, when you need to send that string to an external system (like writing to a file, sending over a network, or printing to a terminal), it needs to be converted into a sequence of bytes. This conversion process is called encoding. If Python attempts to encode a Unicode string using ascii and encounters a character like 'Ă©', it throws this UnicodeEncodeError.

This usually happens in scenarios where:

  • You're performing file I/O (reading or writing) without explicitly specifying an encoding.
  • You're interacting with a database or API that expects a specific encoding, and Python's default or an incorrect explicit encoding is used.
  • You're trying to print a string with non-ASCII characters to a terminal whose encoding isn't correctly set (e.g., to UTF-8).
  • You're explicitly calling .encode('ascii') on a string that contains non-ASCII characters.

Step-by-Step Fix: Explicitly Use UTF-8 Encoding

The core solution is almost always to explicitly specify 'utf-8' as the encoding, as UTF-8 can represent virtually any character from any language.

1. Fixing File I/O

When opening files for reading or writing, always specify the encoding='utf-8' parameter. This ensures that Python correctly handles the conversion of Unicode strings to bytes (when writing) or bytes to Unicode strings (when reading).

Writing to a file:

# This might cause UnicodeEncodeError if 'my_file.txt' is opened with default system encoding (often ASCII on older systems/configs) or if the environment is misconfigured.
# with open('my_file.txt', 'w') as f:
#     f.write('Café latte')

# Correct way: Explicitly specify UTF-8 encoding
content_to_write = 'This is a test with a special character: Café latte and Résumé.'
file_path = 'unicode_example.txt'

try:
    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(content_to_write)
    print(f"Successfully wrote to '{file_path}' with UTF-8 encoding.")
except Exception as e:
    print(f"Error writing file: {e}")

Reading from a file:

Similarly, when reading, specify encoding='utf-8' to correctly interpret the bytes as Unicode characters.

try:
    with open(file_path, 'r', encoding='utf-8') as f:
        read_content = f.read()
    print(f"Successfully read from '{file_path}':")
    print(read_content)
except Exception as e:
    print(f"Error reading file: {e}")

2. Fixing Manual String Encoding/Decoding

If you're manually encoding a string to bytes or decoding bytes to a string, ensure you use 'utf-8'.

my_unicode_string = "Bonjour, comment ça va? Résumé." # Contains 'ç' and 'é'

# This would cause the UnicodeEncodeError:
# encoded_ascii_bytes = my_unicode_string.encode('ascii')

# Correct way: Encode to UTF-8 bytes
encoded_utf8_bytes = my_unicode_string.encode('utf-8')
print(f"UTF-8 encoded bytes: {encoded_utf8_bytes}")

# To get the string back from UTF-8 bytes
decoded_string = encoded_utf8_bytes.decode('utf-8')
print(f"Decoded string: {decoded_string}")

3. Environment Variables for Console/Script Defaults

Sometimes the issue comes from Python's default encoding for console output or its interpretation of the environment. Setting environment variables can help, especially on Linux/macOS, or for script execution.

On Linux/macOS:

Set LANG or LC_ALL to a UTF-8 locale before running your script. You can also set PYTHONIOENCODING for Python 3 specific I/O.

export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
export PYTHONIOENCODING=utf-8
python your_script.py

On Windows:

While less common for Python 3, if you're experiencing issues with console output, you can try setting the console's codepage to UTF-8 (65001) for the current session. Then run your script from that console.

chcp 65001
set PYTHONIOENCODING=utf-8
python your_script.py

4. Database Interactions

If you're inserting or retrieving data from a database, ensure your database connection is configured to use UTF-8. Most database drivers have parameters for this.

Example with psycopg2 (PostgreSQL):

import psycopg2

try:
    conn = psycopg2.connect(
        dbname='your_db',
        user='your_user',
        password='your_password',
        host='localhost',
        client_encoding='UTF8' # Crucial for handling unicode
    )
    cursor = conn.cursor()

    # Insert data with non-ASCII characters
    cursor.execute("INSERT INTO my_table (text_column) VALUES (%s)", ("Café latte",))
    conn.commit()

    # Retrieve data
    cursor.execute("SELECT text_column FROM my_table WHERE text_column = %s", ("Café latte",))
    result = cursor.fetchone()
    print(f"Retrieved from DB: {result[0]}")

    cursor.close()
    conn.close()
    print("Database operation successful with UTF-8.")

except Exception as e:
    print(f"Database error: {e}")

5. Handling Network Requests

Libraries like requests usually handle UTF-8 encoding for JSON payloads automatically. However, if you're dealing with form data or raw strings in URLs, you might need to explicitly encode.

import requests
import urllib.parse

# For JSON payloads, requests typically handles UTF-8 correctly
json_data = {'name': 'Résumé'}
response = requests.post('http://example.com/api', json=json_data)
print(f"API response (JSON): {response.text}")

# For URL parameters, use urllib.parse.quote with UTF-8
query_string_param = "Café"
encoded_param = urllib.parse.quote(query_string_param, encoding='utf-8')
print(f"URL-encoded param: {encoded_param}") # Output: Caf%C3%A9

# Example of using it in a URL
# response = requests.get(f'http://example.com/search?q={encoded_param}')
# print(f"API response (URL): {response.text}")

6. Deliberate Error Handling (Use with Caution)

In rare cases where you absolutely must output to ASCII (e.g., a legacy system that only accepts ASCII) and you know non-ASCII characters might appear, you can use error handlers during encoding. Be aware this means data loss or alteration.

my_string = "RĂ©sumĂ© with a smile 😊"

# 'ignore': drops characters that can't be encoded
ignored_bytes = my_string.encode('ascii', errors='ignore')
print(f"Ignore errors: {ignored_bytes}") # Output: b'Rsum with a smile '

# 'replace': replaces characters with a question mark
replaced_bytes = my_string.encode('ascii', errors='replace')
print(f"Replace errors: {replaced_bytes}") # Output: b'R?sum? with a smile ??'

# 'xmlcharrefreplace': replaces with XML character references
xml_bytes = my_string.encode('ascii', errors='xmlcharrefreplace')
print(f"XML char ref: {xml_bytes}") # Output: b'Résumé with a smile 😊'

Generally, avoid errors='ignore' or errors='replace' unless you explicitly understand and accept the data loss.

Verification Steps

After applying the fix, verify that the issue is resolved:

  • Re-run your code: Execute the problematic Python script or section of code again.

  • Check output:

    If you were writing to a file, open the file with a text editor (like VS Code, Notepad++, Sublime Text) configured to display UTF-8. Ensure all special characters are rendered correctly.

    • If you were printing to the console, verify that the characters appear as expected in your terminal.
    • If you were sending data over a network or to a database, check the receiving end to confirm the data was transmitted and stored with the correct characters.
  • Add assertions: For critical data paths, consider adding unit tests or assertions that verify string content after I/O or transformations.

Tips for Prevention and Debugging

  • Standardize on UTF-8: Make UTF-8 your default encoding for everything: source code files, database connections, web responses, and file I/O. It's the most robust encoding for international text.

  • Check your editor's encoding: Ensure your code editor saves Python files as UTF-8. Most modern editors do this by default, but it's worth checking.

  • Be explicit: Never rely on default encodings when dealing with external data sources. Always specify encoding='utf-8'.

  • Use sys.getdefaultencoding() and sys.getfilesystemencoding(): These can give you insights into what Python thinks its default encodings are, but remember that explicitly specifying 'utf-8' is always safer than relying on these.

  • Debugging with online tools: When I'm working with strings that have been transformed or transmitted, and I suspect encoding issues, I often use online tools to quickly inspect or convert them. For instance, if I'm troubleshooting a UnicodeEncodeError that happens after a string has been URL-encoded, I might use a URL Encoder/Decoder to see how the characters are actually represented.

ToolCraft's URL Encoder/Decoder at https://toolcraft.app/en/tools/developer/url-encoder or their Base64 Encoder/Decoder at https://toolcraft.app/en/tools/developer/base64-encoder are handy for this. They run entirely in the browser, so I don't worry about my data leaving my machine.

Related Error Notes