File tree Expand file tree Collapse file tree 1 file changed +75
-0
lines changed Expand file tree Collapse file tree 1 file changed +75
-0
lines changed Original file line number Diff line number Diff line change 11# csv-diff
22
33Tool for viewing the difference between two CSV files.
4+
5+ Consider two CSV files:
6+
7+ ` one.csv `
8+
9+ id,name,age
10+ 1,Cleo,4
11+ 2,Pancakes,2
12+
13+ ` two.csv `
14+
15+ id,name,age
16+ 1,Cleo,5
17+ 3,Bailey,1
18+
19+ ` csv-diff ` can show a human-readable summary of differences between the files:
20+
21+ $ csv-diff one.csv two.csv --key=id
22+ 1 row added, 1 row removed, 1 row changed
23+
24+ 1 row added
25+
26+ {"id": "3", "name": "Bailey", "age": "1"}
27+
28+ 1 row removed
29+
30+ {"id": "2", "name": "Pancakes", "age": "2"}
31+
32+ 1 row changed
33+
34+ Row 1
35+ age: "4" => "5"
36+
37+ The ` --key=id ` option means that the ` id ` column should be treated as the unique key, to identify which records have changed.
38+
39+ You can also run it using the ` --json ` option to get a machine-readable difference:
40+
41+ $ csv-diff one.csv two.csv --key=id --json
42+ {
43+ "added": [
44+ {
45+ "id": "3",
46+ "name": "Bailey",
47+ "age": "1"
48+ }
49+ ],
50+ "removed": [
51+ {
52+ "id": "2",
53+ "name": "Pancakes",
54+ "age": "2"
55+ }
56+ ],
57+ "changed": [
58+ {
59+ "key": "1",
60+ "changes": {
61+ "age": [
62+ "4",
63+ "5"
64+ ]
65+ }
66+ }
67+ ]
68+ }
69+
70+ You can also import the Python library into your own code like so:
71+
72+ from csv_diff import load_csv, compare
73+ diff = compare(
74+ load_csv(open("one.csv"), key="id"),
75+ load_csv(open("two.csv"), key="id")
76+ )
77+
78+ ` diff ` will now contain the same data structure as the output in the ` --json ` example above.
You can’t perform that action at this time.
0 commit comments