-
Notifications
You must be signed in to change notification settings - Fork 8
/
data.py
57 lines (41 loc) · 1.8 KB
/
data.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
'''
bloodhound 查询的数据解析
比如以下查询会输出大量数据,影响bloodhound 渲染,导出json,然后输出为text 会更好
MATCH p=(u:User)-[r1:GenericAll]->(c:Computer) WHERE u.domain = $result RETURN p
'''
import json
import csv
with open('data.json','r',encoding="utf-8") as f:
data = json.load(f)
# Create a mapping from id to label
id_to_label = {node['id']: node['label'] for node in data['nodes']}
# Create a dictionary to store targets for each source
source_to_targets = {}
# Populate the source_to_targets dictionary
for edge in data['edges']:
source_id = edge['source']
target_id = edge['target']
source_label = id_to_label[source_id]
target_label = id_to_label[target_id]
# Check if the source ID already exists in the dictionary
if source_id in source_to_targets:
source_to_targets[source_id].append(target_label)
else:
source_to_targets[source_id] = [target_label]
with open('output.txt', 'w', encoding='utf-8') as txtfile:
txtfile.write('Source and their counts:\n')
txtfile.write('-' * 60 + '\n')
# First part: Write each source and their counts
for source_id, target_labels in source_to_targets.items():
source_label = id_to_label[source_id]
txtfile.write(f'{source_label}: {len(target_labels)}\n')
txtfile.write('\n')
# Second part: Write each block of data
for source_id, target_labels in source_to_targets.items():
source_label = id_to_label[source_id]
txtfile.write(f'Object: {source_label} => Members:\n')
txtfile.write('-' * 60 + '\n')
for target_label in target_labels: # Loop through each target
txtfile.write('\t' + target_label + '\n')
# Write the count of targets
txtfile.write(f'\nTotal members: {len(target_labels)}\n\n')