Skip to main content

Sort CSV, TSV, JSONL, and text files of any size, with gzip and zstandard support

Project description

sortdx

CI PyPI Python versions License: MIT

A sorting library and command line tool for Python that handles CSV, TSV, JSONL, and text files, including gzip and zstandard compressed files. Large files are sorted on disk through chunking and a k-way merge, so memory usage stays bounded. The core library has no required dependencies and ships full type information.

Installation

pip install sortdx

The core library has no required dependencies. The command line interface and extra features (encoding detection, flexible date parsing) need the optional dependencies:

pip install "sortdx[full]"

Quick start

import sortdx

# Sort a CSV by a numeric column
sortdx.sort_file("data.csv", "sorted.csv", keys=[sortdx.key("age", "num")])

# Sort a JSONL file by timestamp
sortdx.sort_file("logs.jsonl", "sorted.jsonl", keys=[sortdx.key("timestamp", "date")])

# Sort in-memory data
data = [{"name": "Bob", "age": 30}, {"name": "Alice", "age": 25}]
result = list(sortdx.sort_iter(data, keys=[sortdx.key("age", "num")]))

Sort keys

Create sort keys with sortdx.key():

# String sorting (default, case-insensitive)
sortdx.key("name")

# Numeric sorting
sortdx.key("price", "num")

# Date sorting
sortdx.key("created_at", "date")

# Natural sorting: file2 before file10
sortdx.key("filename", "nat")

# Descending order for one key
sortdx.key("salary", "num", desc=True)

# Locale-aware string collation
sortdx.key("name", "str", locale_name="fr_FR.UTF-8")

Columns are addressed by name for dict rows (CSV, JSONL), by integer index for list rows, and None sorts plain text lines as whole items.

Data types

Type Description
str Case-insensitive text sorting (default)
num Numeric sorting for integers and floats
date Date and time sorting; ISO 8601 always works, other formats need python-dateutil
nat Natural sorting where embedded numbers compare by value

Empty and unparseable values sort first within their type.

API

sortdx.sort_file(input_path, output_path, keys, ...)

Sort a file into another file.

Parameter Description
input_path Input file path
output_path Output file path
keys List of sort keys
memory_limit Memory budget such as "512M" or "2G"; larger inputs are sorted on disk (default threshold: 100M)
reverse Reverse the entire sort order
unique Column whose values must be unique in the output
stats Return a SortStats object
stats = sortdx.sort_file(
    "huge.jsonl.gz",
    "sorted.jsonl.gz",
    keys=[sortdx.key("timestamp", "date")],
    memory_limit="512M",
    stats=True,
)
print(f"Processed {stats.lines_processed} lines in {stats.processing_time:.2f}s")

sortdx.sort_iter(data, keys, ...)

Sort an iterable in memory and return an iterator. Accepts the same reverse and unique arguments.

Multi-key sorting

sortdx.sort_file("employees.csv", "sorted.csv", keys=[
    sortdx.key("department"),
    sortdx.key("salary", "num", desc=True),
])

Command line

# Sort a CSV by price, then name; write to a file
sortdx data.csv -o sorted.csv -k price:num -k name:str

# Sort a compressed JSONL file with a memory budget
sortdx logs.jsonl.gz -o sorted.jsonl.gz -k timestamp:date --memory-limit=512M

# Natural sort of a text file to stdout
sortdx filenames.txt --natural

# Keep one row per user id, newest first
sortdx users.jsonl -o unique.jsonl -k created_at:date --reverse --unique=user_id

Key specifications use the format column:type[:option=value], for example price:num, name:str:locale=fr_FR.UTF-8, or rank:num:desc=true.

Supported formats

Format Extensions
CSV .csv (delimiter auto-detected)
TSV .tsv, .tab
JSON Lines .jsonl, .ndjson, .json
Plain text .txt and anything else
Compression .gz, .gzip, .zst, .zstd (zstandard requires pip install zstandard)

Development

git clone https://github.com/Okymi-X/sortdx.git
cd sortdx
pip install -e ".[full,dev]"
pytest

See CONTRIBUTING.md for the full set of checks (formatting, lint, type checking) and the contribution guidelines.

Changelog

Release history is documented in CHANGELOG.md.

License

MIT License. See the LICENSE file for details.

Contributing

Contributions are welcome. Please open an issue or submit a pull request on GitHub.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sortdx-0.2.0.tar.gz (22.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sortdx-0.2.0-py3-none-any.whl (18.0 kB view details)

Uploaded Python 3

File details

Details for the file sortdx-0.2.0.tar.gz.

File metadata

  • Download URL: sortdx-0.2.0.tar.gz
  • Upload date:
  • Size: 22.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for sortdx-0.2.0.tar.gz
Algorithm Hash digest
SHA256 a57c90108f5454f9c17b0b6cf327e4f2b438e8382ce1b4b9ab423a591e6bae5f
MD5 60480b0b3a3a814484a0381ba3efb76e
BLAKE2b-256 8b8032779bd120d63485064e31cf8c56e15616d323d4d04fb0fa31b28e704fa5

See more details on using hashes here.

File details

Details for the file sortdx-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: sortdx-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 18.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for sortdx-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fe1d66641f0cc8896bb77d0d3fb5a11127373f91f9c31094eb22970390fb1cff
MD5 87d8e4d141e74bc4c28b604067d9ed5f
BLAKE2b-256 c5e1b4ab814aefce212e2cdb26c11c1e71b121df2cd96db84a59401632feb474

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page