csv-diff is a Python CLI tool and library for efficiently comparing the semantic contents of two CSV, TSV, or JSON files. It identifies added, removed, and changed rows based on a specified key, ignoring cosmetic differences like row and column ordering. The library is actively maintained with regular updates addressing features and bug fixes, with its current version being 1.2.
pip install csv-diffVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use `csv-diff` programmatically to compare two in-memory CSV datasets. It loads the data using `load_csv`, specifying 'id' as the unique key, and then uses `compare` to generate a dictionary detailing added, removed, and changed rows and columns.
Always provide the `--key` option when using the CLI, or the `key` parameter to `load_csv` when using the library.
Ensure your `csv-diff` version is 1.0 or newer. If you relied on the old (buggy) behavior, review your diffs after updating.
Use the `--format` CLI option or ensure your data loading logic explicitly handles the expected input format to avoid unexpected parsing behavior.
For programmatic consumption of diff results, use the dictionary output from the `compare` function rather than parsing the CLI's human-readable text output. If you must parse CLI output, ensure your parsing logic is robust to format changes or use `--json` output.
Ensure the key column name provided to `csv-diff` (using `--key` in CLI or `index_columns` in the library) exactly matches the header in both CSV files. Inspect column names for invisible characters or unexpected encoding (e.g., check for UTF-8 BOM if encountering issues, by opening the file with `encoding='utf-8-sig'` if using the library).
Examine the CSV file, particularly near line 'X', for structural inconsistencies. Use a text editor to identify and correct issues like misplaced commas, unclosed quotes, or unexpected characters. If using a non-comma delimiter, ensure it's consistently applied.
Verify that the file path is correct, including the file name and extension. Ensure the file exists at the specified location and that `csv-diff` has read permissions for the file and its parent directories. Use absolute paths to avoid issues with relative working directories.
Pre-convert the CSV file to UTF-8 encoding using a text editor or another script. If using `csv-diff` as a Python library, open the files with the correct `encoding` parameter (e.g., `open(filename, 'r', encoding='latin1')` or `encoding='windows-1252'`) before passing the file-like objects to `csvdiff.diff_files` or `csvdiff.diff_records`.