#!/usr/bin/env python3 """Сравнение двух CSV по ключевым колонкам.""" from __future__ import annotations import argparse import json from pathlib import Path import pandas as pd def side_record(row: pd.Series, columns: list[str], side: str) -> dict[str, str]: record = {} for column in columns: suffixed = f"{column}_{side}" if suffixed in row.index: value = row[suffixed] elif column in row.index: value = row[column] else: value = "" record[column] = "" if pd.isna(value) else str(value) return record def main() -> int: parser = argparse.ArgumentParser(description="Сравнить две таблицы") parser.add_argument("--left", type=Path, required=True, help="Старая таблица") parser.add_argument("--right", type=Path, required=True, help="Новая таблица") parser.add_argument("--key", required=True, help="Ключевые колонки через запятую") parser.add_argument("--ignore-columns", default="", help="Колонки, которые не сравнивать") parser.add_argument("--output", type=Path, default=Path("diff.csv"), help="Отчёт о различиях") parser.add_argument("--json", type=Path, help="Сводка в JSON") parser.add_argument("--force", action="store_true", help="Перезаписать выходные файлы") args = parser.parse_args() left_path = args.left.expanduser().resolve() right_path = args.right.expanduser().resolve() target = args.output.expanduser().resolve() for path in (left_path, right_path): if not path.is_file(): parser.error(f"Файл не найден: {path}") if left_path == right_path: parser.error("Левая и правая таблицы совпадают") if target in {left_path, right_path}: parser.error("Отчёт не должен совпадать с входными файлами") paths = [target] if args.json: paths.append(args.json.expanduser().resolve()) if len(set(paths)) != len(paths): parser.error("Выходные файлы не должны совпадать") for path in paths: if path.exists() and not args.force: parser.error(f"Файл уже существует: {path}. Добавьте --force") keys = [item.strip() for item in args.key.split(",") if item.strip()] ignored = {item.strip() for item in args.ignore_columns.split(",") if item.strip()} if not keys: parser.error("Укажите ключевые колонки в --key") left = pd.read_csv(left_path, dtype=str, keep_default_na=False, encoding="utf-8-sig") right = pd.read_csv(right_path, dtype=str, keep_default_na=False, encoding="utf-8-sig") for name, frame in (("left", left), ("right", right)): missing = [column for column in keys if column not in frame.columns] if missing: parser.error(f"В {name} нет ключевых колонок: " + ", ".join(missing)) if frame.duplicated(subset=keys).any(): parser.error(f"В {name} есть повторяющиеся ключи") common = [ column for column in left.columns if column in right.columns and column not in keys and column not in ignored ] merged = left.merge(right, on=keys, how="outer", suffixes=("_left", "_right"), indicator=True) records: list[dict[str, str]] = [] added = removed = changed_keys = changed_cells = 0 for _, row in merged.iterrows(): status = row["_merge"] key_values = {key: str(row[key]) for key in keys} if status == "left_only": removed += 1 records.append({**key_values, "change": "removed", "column": "", "left_value": json.dumps(side_record(row, list(left.columns), "left"), ensure_ascii=False), "right_value": ""}) elif status == "right_only": added += 1 records.append({**key_values, "change": "added", "column": "", "left_value": "", "right_value": json.dumps(side_record(row, list(right.columns), "right"), ensure_ascii=False)}) else: left_record = side_record(row, list(left.columns), "left") right_record = side_record(row, list(right.columns), "right") differences = [column for column in common if left_record[column] != right_record[column]] if differences: changed_keys += 1 for column in differences: changed_cells += 1 records.append({**key_values, "change": "changed", "column": column, "left_value": left_record[column], "right_value": right_record[column]}) columns = [*keys, "change", "column", "left_value", "right_value"] report = pd.DataFrame(records, columns=columns) target.parent.mkdir(parents=True, exist_ok=True) report.to_csv(target, index=False, encoding="utf-8-sig") summary = { "left_rows": len(left), "right_rows": len(right), "added": added, "removed": removed, "changed_keys": changed_keys, "changed_cells": changed_cells, "unchanged": len(merged) - added - removed - changed_keys, } if args.json: json_path = args.json.expanduser().resolve() json_path.parent.mkdir(parents=True, exist_ok=True) json_path.write_text(json.dumps(summary, ensure_ascii=False, indent=2) + "\n", encoding="utf-8") print(f"Добавлено: {added}") print(f"Удалено: {removed}") print(f"Изменено ключей: {changed_keys}, ячеек: {changed_cells}") print(f"Без изменений: {summary['unchanged']}") print(f"Отчёт: {target}") return 0 if __name__ == "__main__": raise SystemExit(main())