Web-compatible encoding and decoding library
Actively maintained. Last release 7 weeks ago, commits in 21 of the last 26 weeks. A safe choice.
$
pip install endecendec
Web-compatible encoding and decoding library
endec uses encoding_rs (which powers Firefox) under the hood.
Supported Python versions
- CPython 3.9 or higher
- PyPy 3.11 or higher
Installation
Using uv package manager (recommended)
uv add endec
Using Poetry package manager
poetry add endec
Using pip
pip install endec
Examples
Codecs
Please refer to WHATWG Web Encoding Standard for available codecs.
Encode
import endec
utf8_bytes = endec.encode("こんにちは")
assert utf8_bytes == b"\xe3\x81\x93\xe3\x82\x93\xe3\x81\xab\xe3\x81\xa1\xe3\x81\xaf"
iso2022jp_bytes = endec.encode("㊤㊥㊦", "iso-2022-jp")
assert iso2022jp_bytes == b"\x1b$B-e-f-g\x1b(B"
"㊤㊥㊦".encode("iso-2022-jp") # Standard Library `encode`
# UnicodeEncodeError: 'iso2022_jp' codec can't encode character '\u32a4' in position 0: illegal multibyte sequence
Decode
import endec
utf8_str = endec.decode(b"\xe3\x81\x93\xe3\x82\x93\xe3\x81\xab\xe3\x81\xa1\xe3\x81\xaf")
assert utf8_str == "こんにちは"
iso2022jp_str = endec.decode(b"\x1b$B-e-f-g\x1b(B", "iso-2022-jp")
assert iso2022jp_str == "㊤㊥㊦"
b"\x1b$B-e-f-g\x1b(B".decode("iso-2022-jp") # Standard Library `decode`
# UnicodeDecodeError: 'iso2022_jp' codec can't decode bytes in position 3-4: illegal multibyte sequence