Text Processing

Decode bytes of unknown encoding with Charset Normalizer, and fix mis-decoded text with ftfy. RapidFuzz fuzzy-matches at scale; small jobs need only difflib.

How to choose:

  • Bytes in an unknown encoding: Charset Normalizer
  • Text already decoded with the wrong encoding (mojibake): ftfy
  • Fuzzy matching across many strings, or with words in a different order: RapidFuzz
  • Diffs, or close matches from a short list with no dependency: difflib
  • Bytes that may not be text, identified by MIME type: chardet
  • Translating an app, or formatting numbers and dates for a locale: Babel
  • Syntax highlighting for source code: Pygments
  • A grammar of your own: pyparsing to pull data from well-formatted text, parsy for a small language
  • SQL: sqlparse to split and format it, SQLGlot to translate it between dialects
  • Phone numbers: phonenumbers
  • URL slugs: python-slugify, or Unidecode for plain ASCII text
  • Short, URL-safe IDs: shortuuid

Listed in editorial order, grouped by use case. Click a column header to re-sort the whole list.

Press / to search. Tags open their category. Click a row for details.

Search and filter

Results

Row number Tags

Diffs 1 project

(Python standard library) Helpers for computing deltas.

Encoding and Unicode 3 projects

Universal character encoding detector, and a dependency of requests.
Python character encoding detector.
chardet/github.com/chardet/chardet /2,690 stars /
Makes Unicode text less broken and more consistent automagically.
rspeer/github.com/rspeer/python-ftfy /4,068 stars /

Fuzzy Matching 1 project

Rapid fuzzy string matching using various string metrics, with a C++ core.
rapidfuzz/github.com/rapidfuzz/RapidFuzz /4,235 stars /

Internationalization 1 project

An internationalization library for Python.
python-babel/github.com/python-babel/babel /1,473 stars /

Parser 6 projects

A generic syntax highlighter.
pygments/github.com/pygments/pygments /2,227 stars /
A Python library for creating PEG parsers.
pyparsing/github.com/pyparsing/pyparsing /2,500 stars /
A non-validating SQL parser.
A no-dependency SQL parser, transpiler, optimizer, and engine.
tobymao/github.com/tobymao/sqlglot /9,842 stars /
Parsing, formatting, storing and validating international phone numbers.
Easy, generic parser combinator library for creating parsers.

Transliteration and Slugs 2 projects

A Python slugify library that translates unicode to ASCII.
un33k/github.com/un33k/python-slugify /1,632 stars /
ASCII transliterations of Unicode text.
avian2/github.com/avian2/unidecode /610 stars /

Unique identifiers 1 project

A generator library for concise, unambiguous and URL-safe UUIDs.

Text Processing guide

Charset Normalizer

Pick Charset Normalizer to read bytes whose encoding you don't know. Requests depends on it, so it's often installed already. It doesn't try to uncover the originating encoding: it finds the one that best suits the content. Pass your bytes to from_bytes(), or a file to from_path(), and call .best() on the result for the most likely decoding.

chardet

Pick chardet when some of your bytes may not be text and you want to know what they are. Besides the encoding, every result carries a MIME type, so it can tell you a file is an image or an archive. Start with chardet.detect(), which returns the encoding with a confidence score.

ftfy

Use ftfy on text that was already decoded with the wrong encoding, like ✔ where ✔ belongs. It's a mojibake detector and fixer, not an encoding detector, so it takes a str, never bytes. Call ftfy.fix_text(), which applies every fix it can and returns the repaired text.

RapidFuzz

Pick RapidFuzz to fuzzy-match strings in bulk, or when the same words can come in a different order. Scorers like token_sort_ratio() sort the words before comparing. To match a string against a list, use the process module, whose extractOne() returns the best match.

difflib

Use difflib, in the standard library, for diffs and for small fuzzy-matching jobs that shouldn't add a dependency. unified_diff() shows just the lines that changed plus a few lines of context. get_close_matches() returns the best "good enough" matches for a word from a list.

Babel

Pick Babel to translate a Python app, or to format numbers and dates for each user's locale. It works with gettext message catalogs and the CLDR locale data. Formatting functions like format_decimal() take a locale argument. Translations go through the pybabel command, which extracts your messages, keeps a catalog per language, and compiles it for your app.

Pygments

Pick Pygments to syntax-highlight source code, for code hosting, forums, wikis, or any app that shows code. Pass your code, a lexer for its language, and a formatter to highlight() to get HTML, and generate the matching CSS with get_style_defs(). Pygments makes no guarantees on execution time, so on user input, run it in a process you stop after a short timeout.

pyparsing

Pick pyparsing to pull data out of text in a known format, such as reports or command strings: its docs say to use it on strings assumed to be well-formatted. You build the grammar in Python code, and it handles whitespace, quoted strings, and comments for you. Name the parts you want, and read them back by name from the results.

parsy

Pick parsy to write a parser for a small language as readable code. It excels at easy-to-read parsers for relatively small languages, with a minimal core and no built-in helpers for specific grammars. Its docs also say projects with demanding speed or error-message needs may have to look elsewhere. Start from primitives like regex(), combine them into larger parsers, and call .parse() on your input.

sqlparse

Pick sqlparse to split, format, or parse SQL without validating it. It makes no assumptions about a particular SQL dialect, so vendor extensions and templated SQL parse too. Its three main functions are split(), format(), and parse().

SQLGlot

Pick SQLGlot to translate SQL between dialects, or to read and rewrite a query as an expression tree. Start with sqlglot.transpile(), which takes your SQL with its read and write dialects. If you parse with parse_one(), always pass the dialect when you know it, or valid SQL may fail to parse.

phonenumbers

Pick phonenumbers to parse, validate, and format phone numbers. It's a Python port of Google's libphonenumber. Call parse() with the number and the country it's dialed from, check the result with is_valid_number(), and format it with format_number(), such as E.164.

python-slugify

Pick python-slugify to turn a title into a URL slug: slugify("C'est déjà l'été.") returns c-est-deja-l-ete.

Unidecode

Pick Unidecode to turn Unicode text into plain ASCII, for legacy code that can't handle Unicode or for machine identifiers that stay readable. unidecode('kožušček') returns kozuscek. Unidecode is GPL, and for URL slugs, python-slugify is the pick.

shortuuid

Pick shortuuid for short, non-sequential IDs that users see, like in URLs. It makes a UUID with the standard library's uuid module and translates it to base57, leaving out look-alike characters such as l, 1, I, O, and 0. Call shortuuid.uuid() for a new ID.

Declared encodings first

Before you hand bytes to Charset Normalizer or chardet, decode them with the encoding they declare. Charset Normalizer's FAQ says to use the detector as a last resort, when you're left with no clue.

Know a project that belongs here?

Tell us what it does and why it stands out.