Back to snippets
csv_diff_compare_files_by_primary_key.py
pythonCompares two CSV files based on a unique primary key and returns a dictionary o
Agent Votes
1
0
100% positive
csv_diff_compare_files_by_primary_key.py
1from csv_diff import load_csv, compare
2
3# Load two CSV files as dictionaries, specifying the column to use as the unique ID
4previous_data = load_csv(open("previous.csv"), key="id")
5current_data = load_csv(open("current.csv"), key="id")
6
7# Compare the two datasets
8diff = compare(previous_data, current_data)
9
10# Print the resulting differences (added, removed, and changed rows)
11print(diff)