Redlines
Redlines is a Python library that compares two strings or blocks of text, producing human-readable differences or 'deltas' similar to Microsoft Word's track changes. It provides structured output showing detailed change information, positions, and statistics, supporting various formats like JSON (default for CLI), Markdown, HTML, and rich terminal display. The current version is 0.6.1, and the project exhibits an active release cadence.
Warnings
- breaking The command-line interface (CLI) behavior changed significantly in v0.6.0. It is now command-less by default and outputs JSON. Previous versions might have had different default outputs or required specific subcommands.
- gotcha This `redlines` library is for comparing general text strings. There is a similarly named, but distinct, `Python-Redlines` project (not on PyPI) focused specifically on `.docx` file comparison. Ensure you are installing the correct library for your use case.
- gotcha Advanced features like intelligent sentence detection (for Python 3.11+) and Levenshtein distance statistics require installing optional dependencies using `pip install redlines[nupunkt]` or `pip install redlines[levenshtein]` respectively. These features are not available with a basic `pip install redlines`.
Install
-
pip install redlines -
pip install redlines[nupunkt] -
pip install redlines[levenshtein]
Imports
- Redlines
from redlines import Redlines
Quickstart
from redlines import Redlines
old_text = "The quick brown fox jumps over the lazy dog."
new_text = "The quick brown fox walks past the lazy cat."
# Create a Redlines object
comparator = Redlines(old_text, new_text)
# Get markdown output with <del>/<ins> tags
markdown_output = comparator.output_markdown
print(f"Markdown Output:\n{markdown_output}\n")
# Get JSON output (structured data)
json_output = comparator.output_json()
print(f"JSON Output:\n{json_output}\n")
# Example of using the compare method for multiple comparisons
alternate_new_text = "The quick brown fox sleeps near the lazy dog."
compare_output = comparator.compare(alternate_new_text)
print(f"Comparison with alternate text:\n{compare_output}")