How to extract labels from a GML file with networkx?

17 views Asked by At

I am looking to extract all nodes with label as super_technique from my GML file, which looks like this:

 node [
    id 17
    label "T1592"
    name "Gather Victim Host Information"
    types "super_technique"
  ]
  node [
    id 18
    label "T1592/001"
    name "Hardware"
    types "sub_technique"
  ]
  node [
    id 19
    label "T1592/002"
    name "Software"
    types "sub_technique"
  ]

Here is my current code on Python:

import csv
import networkx as nx

# Load the GML file
tree = nx.read_gml('Tactic_Technique_Reference_Example.gml')

technique_dict = {}
for node, data in tree.nodes(data=True):
    types = data['types']
    if types == "super_technique":
        label = data['label']
        id = data['id']
        technique_dict[label] = id
        print(technique_dict[label])

However, when I tried to run the code, I got a KeyError: 'label'. Therefore, I would like to ask that is there any way to extract super_technique labels from my GML file?

0

There are 0 answers