Registry / serialization / ansi2txt

ansi2txt

JSON →
library0.2.0pypypi✓ verified 83d ago

ansi2txt is a small Python library designed to convert ANSI escape sequences, commonly found in terminal output, into plain text. It effectively strips out color codes and other formatting, leaving only the readable content. The current version is 0.2.0, and releases are infrequent, indicating a stable, feature-complete library for its specific purpose.

pip install ansi2txt
INSTALL
IMPORT
SIG · ANSI2TXT
A
ansi2txt
serializationpythonv0.2.0
Install
1.5s avg
Import
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.2.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.000s · 17.8MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 1.5s · import 0.000s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

getchar
from ansi2txt import getchar
from ansi2txt import ansi2txt
putchar
from ansi2txt import putchar
main
from ansi2txt import main

This quickstart demonstrates how to import the `ansi2txt` function and use it to convert an ANSI-formatted byte string (or encoded unicode string) into plain text, stripping all control characters.

from ansi2txt import ansi2txt # Example ANSI colored string (bytes) ansi_text_bytes = b'\x1b[31mHello, \x1b[32mWorld!\x1b[0m This is \x1b[4mcolored\x1b[0m text.' # Convert to plain text plain_text = ansi2txt(ansi_text_bytes) print(f"Original ANSI: {ansi_text_bytes.decode('utf-8')}") print(f"Plain text: {plain_text}") # Example with unicode string (needs encoding first) ansi_text_str = '\u001b[33mWarning:\u001b[0m Something happened.' plain_text_from_str = ansi2txt(ansi_text_str.encode('utf-8')) print(f"Plain text from string: {plain_text_from_str}")
Debug
Known issues
gotchaThe `ansi2txt` function primarily expects byte strings (`bytes`) as input, reflecting its common use case with terminal output. While it can handle ASCII strings, passing a unicode string (`str`) directly will raise a TypeError if it contains non-ASCII characters or if it's not explicitly encoded.
fix
Ensure your input is a `bytes` object. If you have a unicode string, encode it first: `ansi2txt(your_string.encode('utf-8'))`.
affects: All versions (0.1.0+)
gotchaThe library is designed for common ANSI SGR (Select Graphic Rendition) codes (colors, bold, underline). It may not fully support more complex or non-standard ANSI sequences, such as cursor positioning, screen clearing, or device control sequences, and might leave them in the output or strip them unexpectedly.
fix
For specific edge cases with complex ANSI sequences, review the source code or consider pre-processing your input to handle specific sequences if they are critical and not stripped correctly.
affects: All versions (0.1.0+)
Upgrade
Version history
0.2.0latest on PyPI · released Feb 23, 2023
Audit
Dependencies

No dependency data recorded yet.

Agent activity
29 hits · last 30 days
node
27
OpenAI (training)
1
Resources