---
source: https://iinventedlight.com/library/notes/python-codecs.md
title: "Python codecs: encodings and the codec registry"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF-2.0 (Python docs); PSF-2.0 (CPython docs); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Python docs, codecs — Codec registry and base classes; CPython docs, codecs.rst. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: codecs"
tags: [python, codecs, encoding, utf-8]
client: Librarian
sources:
  - id: S1
    title: "codecs — Codec registry and base classes"
    publisher: Python docs
    url: https://docs.python.org/3/library/codecs.html
    accessed: 2026-11-18
    license: "PSF-2.0"
  - id: S2
    title: "codecs.rst"
    publisher: CPython docs
    url: https://raw.githubusercontent.com/python/cpython/main/Doc/library/codecs.rst
    accessed: 2026-11-18
    license: "PSF-2.0"
---
# Python codecs: encodings and the codec registry
**Answer:** The codecs module defines base classes for standard Python codecs and provides access to the codec registry. Prefer `open()` from the io module for encoded text files. `codecs.getencoder` / `getdecoder` look up an encoding and return the encoder or decoder function.

## Examples

```python
import codecs
enc = codecs.getencoder("utf-8")
print(enc("café"))
```

## Rules

- `getencoder` raises `LookupError` if the encoding cannot be found.
- Most standard codecs are text encodings that encode text to bytes.

## See also

- [python-io](https://iinventedlight.com/library/notes/python-io/)
- [python-pathlib](https://iinventedlight.com/library/notes/python-pathlib/)
- [python-json](https://iinventedlight.com/library/notes/python-json/)

## Claims

- id: C1
  claim: codecs defines base classes for standard Python codecs.
  source: S1
  evidence: "This module defines base classes for standard Python codecs"

- id: C2
  claim: getencoder looks up a codec and returns its encoder function.
  source: S1
  evidence: "Look up the codec for the given encoding and return its encoder function"

- id: C3
  claim: The rst source describes the same codec base classes.
  source: S2
  evidence: "This module defines base classes for standard Python codecs"

## Sources

- [S1] codecs — Codec registry and base classes. Python docs. https://docs.python.org/3/library/codecs.html . License: PSF-2.0. Accessed 2026-11-18.

- [S2] codecs.rst. CPython docs. https://raw.githubusercontent.com/python/cpython/main/Doc/library/codecs.rst . License: PSF-2.0. Accessed 2026-11-18.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python docs, codecs — Codec registry and base classes; CPython docs, codecs.rst. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
