-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsorter.py
57 lines (41 loc) · 1.91 KB
/
sorter.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import argparse
import json
from collections import defaultdict
from pypinyin import pinyin, Style
def main():
parser = argparse.ArgumentParser(description='Process JSON data with comments')
parser.add_argument('-f', '--file', type=str, help='Input file path')
parser.add_argument('-s', '--sort_key', type=str, default='name', help='Key to sort the data')
parser.add_argument('-g', '--group_key', type=str, default='station', help='Key to group the data')
parser.add_argument('-o', '--output', type=str, help='Output file name')
args = parser.parse_args()
with open(args.file, "r", encoding='utf-8') as file:
json_data_str = file.read()
parsed_json_data = json.loads(json_data_str)
existed_keys = []
json_data = []
for ele in parsed_json_data:
if isinstance(ele, dict):
if ele['id'] in existed_keys:
continue
json_data.append(ele)
existed_keys.append(ele['id'])
sorted_data = sorted(json_data, key=lambda x: pinyin(x.get(args.sort_key), style=Style.NORMAL))
grouped_data = defaultdict(list)
for item in sorted_data:
grouped_data[item[args.group_key]].append(item)
sorted_grouped_data = dict(sorted(grouped_data.items(), key=lambda x: x[0]))
output_json = []
for group_key, group in sorted_grouped_data.items():
output_json.append("-" * 15 + group_key + "-" * 14 + ">")
output_json.extend(group)
output_json.append("<" + "-" * 14 + group_key + "-" * 15)
output_json_str = json.dumps(output_json, ensure_ascii=False, indent=2, separators=(',', ': '))
if args.output:
output_file = args.output
else:
output_file = args.file.replace('.json', '_out.json')
with open(output_file, 'w', encoding='utf-8') as outfile:
outfile.write(output_json_str)
if __name__ == "__main__":
main()