-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiff_csv_for_f.py
executable file
·43 lines (36 loc) · 1.7 KB
/
diff_csv_for_f.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env python3
import argparse, csv, os, json
from collections import defaultdict
def extract_items(csv_reader, to_dict, items):
for row in csv_reader:
name = row['name']
for key, val in row.items():
if key in items:
to_dict[name][key] = val
def diff_csv(ref, exp, items, output):
with open(ref, 'r') as ref_file, open(exp, 'r') as exp_file, open(output, 'w') as json_file:
ref_reader = csv.DictReader(ref_file)
exp_reader = csv.DictReader(exp_file)
ref_dict = defaultdict(lambda:defaultdict(str))
exp_dict = defaultdict(lambda:defaultdict(str))
extract_items(ref_reader, ref_dict, items)
extract_items(exp_reader, exp_dict, items)
json_dict = defaultdict(lambda:defaultdict(lambda:defaultdict(str)))
for name, _ in ref_dict.items():
for key in _.keys():
ref_val = ref_dict[name][key]
exp_val = exp_dict[name][key]
if exp_val != ref_val:
json_dict[name]['ref'][key] = ref_val
json_dict[name]['exp'][key] = exp_val
json.dump(json_dict, json_file, indent=2)
json_file.write('\n')
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Show difference between *.f.csv for interesting items')
parser.add_argument('ref', help='reference *.f.csv')
parser.add_argument('exp', help='experiment *.f.csv')
parser.add_argument('--items', required=True, help='items to compare')
parser.add_argument('-o', '--output', required=True, help='output json file')
args = parser.parse_args()
diff_csv(args.ref, args.exp, args.items.split(','), args.output)