Extracting data from a GML file

18,440

If you can use NetworkX (Python) you can read the file (t.gml is your file) like this and get the mapping

In [1]: import networkx as nx

In [2]: G = nx.read_gml('t.gml')

In [3]: G.node
Out[3]: 
{0: {'id': 0, 'label': u'24'},
 1: {'id': 1, 'label': u'25'},
 2: {'id': 2, 'label': u'26'},
 3: {'id': 3, 'label': u'27'},
 4: {'id': 4, 'label': u'20'},
 5: {'id': 5, 'label': u'21'}}

e.g.

In [4]: G.node[0]['label']
Out[4]: u'24'
Share:
18,440
adwaraki
Author by

adwaraki

Updated on June 08, 2022

Comments

  • adwaraki
    adwaraki about 2 years

    I have a GML(Graph (not Graffiti) Modeling Language) file from which I want to create a mapping of IDs to labels. I am at a loss as to how I can do this since my list manipulations do not seem to work. I tried using the example which specifies how to use two delimiters, but it would not work on my file.

    Can someone please guide me?

    The GML file is arranged as follows:

    graph [
      node [
        id 0
        label "24"
      ]
      node [
        id 1
        label "25"
      ]
      node [
        id 2
        label "26"
      ]
      node [
        id 3
        label "27"
      ]
      node [
        id 4
        label "20"
      ]
      node [
        id 5
        label "21"
      ]
    (... some more nodes)
      edge [
        source 0
        target 75
        weight 4
      ]
      edge [
        source 0
        target 68
        weight 2
      ]
    (many more edges)
    ]
    

    I want to extract the data in each node block and create a mapping of the node ID to the node label. For example, the first node's mapping would be 0->24 and so on. I am not worried about the edge information.

    Thanks in advance for any help.

  • mlg4080
    mlg4080 over 9 years
    How do you change the file path using nx.read_gml? I'm uncertain where it is searching for gml file, but it cannot seem to find it.