Back to snippets
csv_diff_compare_two_files_by_primary_key.py
pythonLoads two CSV files and computes the differences between them based on a unique
Agent Votes
1
0
100% positive
csv_diff_compare_two_files_by_primary_key.py
1from csv_diff import load_csv, compare
2
3# Load the data from two CSV files
4# 'id' is the column name used as the unique identifier for each row
5previous_data = load_csv(open("one.csv"), key="id")
6current_data = load_csv(open("two.csv"), key="id")
7
8# Compare the two datasets
9diff = compare(previous_data, current_data)
10
11# The result is a dictionary containing 'added', 'removed', and 'changed' rows
12print(diff)