Skip to main content

A package for comparing CSV files

Project description

csv-diff-tool

License: MIT Python 3.8+

A Python library for comparing CSV files with detailed output including extra columns, extra rows, and mismatched cell values. Supports automatic encoding detection, data transformations, and flexible input methods.

Installation

pip install csv-diff-tool

Quick Start

Compare two CSV files

from csv_diff_tool import CSVComparer

result = CSVComparer.from_files("report_v1.csv", "report_v2.csv").compare("id")

print(result.match_result)              # True if files are identical
print(result.extra_cols_in_first_file)  # Columns only in first file
print(result.extra_rows_in_second_file) # Rows only in second file
print(result.mismatched_rows)           # Cell-level differences

Compare from in-memory data

from csv_diff_tool import CSVComparer

file_1 = ["id,name,score", "1,Alice,95", "2,Bob,87"]
file_2 = ["id,name,score", "1,Alice,95", "2,Bob,90", "3,Carol,88"]

result = CSVComparer.from_lines(file_1, file_2).compare("id")

print(result)
# First file: init from lines
# Second file: init from lines
# Match result: False
# Extra rows in second file
# 	3
# Mismatched rows
# 	row: 2, column: score, first: 87, second: 90

Transform data before comparing

from csv_diff_tool import CSVComparer

comparer = CSVComparer.from_files("old.csv", "new.csv")

# Strip whitespace from all cells
comparer.strip_whitespace()

# Drop columns you don't care about
comparer.drop_columns(["timestamp", "metadata"])

# Drop specific rows
comparer.drop_rows("status", ["deleted", "archived"])

# Apply custom transforms
comparer.apply_transform("price", lambda row: str(round(float(row["price"]), 2)))

result = comparer.compare("id")

Work with CSVParser directly

from csv_diff_tool import CSVParser

parser = CSVParser.from_file("data.csv")

# Inspect the data
print(parser.column_names)
print(parser.get_row("id", "42"))
print(parser.row_values_in_column("name"))

# Modify and write back
parser.set_value("42", "status", "updated")
parser.write_to_file()

API Overview

CSVComparer

The main comparison class. Create instances using factory methods:

  • CSVComparer.from_files(first_file, second_file) - Load from file paths
  • CSVComparer.from_lines(first_lines, second_lines) - Load from lists of strings
  • CSVComparer.from_csv_parsers(parser1, parser2) - Load from CSVParser objects

Transform methods (applied to both files):

  • strip_whitespace() - Remove leading/trailing whitespace
  • drop_columns(column_names) - Remove specified columns
  • drop_rows(column, values) - Remove rows matching values
  • drop_rows_by(predicate) - Remove rows matching a function
  • apply_transform(column, func) - Apply a function to a column

Compare:

  • compare(index_column) - Returns a CSVCompareOutput with the results

CSVParser

Parses CSV data into a list of dictionaries with column-aware operations:

  • CSVParser.from_file(path) - Load from file (auto-detects encoding)
  • CSVParser.from_lines(lines) - Load from list of strings
  • CSVParser.from_csv_text(text) - Load from a single string
  • get_row(column, value) / get_rows(column, value) - Query rows
  • get_value(row_id, column) / set_value(row_id, column, value) - Cell access
  • write_to_file() - Write modifications back to the source file

CSVCompareOutput

A dataclass containing comparison results:

Field Type Description
match_result bool True if files are identical
extra_cols_in_first_file List[str] Columns only in the first file
extra_cols_in_second_file List[str] Columns only in the second file
extra_rows_in_first_file List[str] Rows only in the first file
extra_rows_in_second_file List[str] Rows only in the second file
mismatched_rows List[Dict] Cell-level mismatches with row, column, first, second

Contributing

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

# Clone and install for development
git clone https://github.com/ashishnarmen/csv-diff-tool.git
cd csv-diff-tool
pip install -e ".[test]"

# Run tests with unittest and coverage
coverage run -m unittest discover tests/
coverage report -m

# Or, if you prefer pytest (install separately)
pip install pytest pytest-cov
pytest tests/ -v --cov=csv_diff_tool

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

csv_diff_tool-0.1.1.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

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

csv_diff_tool-0.1.1-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file csv_diff_tool-0.1.1.tar.gz.

File metadata

  • Download URL: csv_diff_tool-0.1.1.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for csv_diff_tool-0.1.1.tar.gz
Algorithm Hash digest
SHA256 74a73556cc65c0c24f64a78fc90fca5763d1354ed73279cf1b01d311b270fd44
MD5 047ec45331d64ff79800860f8e115b80
BLAKE2b-256 ba43c64834421ecaef2805e0f88034d71d256556f99d52dad965d3cb7cbb3612

See more details on using hashes here.

File details

Details for the file csv_diff_tool-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: csv_diff_tool-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 11.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for csv_diff_tool-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ef4f2a3a27d21186191fe88c4356cc2942e9d709b9ac79619af9263c3c8cd247
MD5 66d8907d251f557bef789f1f3ca3fc89
BLAKE2b-256 c2fd50dd3260ca624371e33fc44ce2b7317c375b7e5a251459791a841d3a7f95

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