I am trying to read a GML (graph modelling language) file from networkX, but it returns a ParseError.
I just wrote a couple of lines of code:
import networkx as nx
G = nx.read_gml('test.gml')
print G.node
This is the GML file:
graph [
directed 1
node [
id 0
label 1
shape
name "square"
center (132, 258)
]
node [
id 1
label 2
shape
name "triangle"
center (132, 128)
]
edge [
source 0
target 1
relation "below"
]
edge [
source 1
target 0
relation "above"
]
]
And this is the error I got:
ParseException: Expected "]" (at char 23), (line:3, col:3)
However, I don't see any ]
missing
Immediate Problem
The parser is getting confused with your input. I have never used gml before but I built your gml file up one piece at a time and found two problems.
shape
key with no value(132, 258)
Assuming that this reference is correct, there are 4 possible value types:
Therefore
(132, 258)
is not valid and I believe having no value also would not be valid.Try the below commented-out gml and it will load.
Alternative Solution
I found somewhere that
nx.write_gml
does not support mixed graphs including nested graphs. So even if you are creating the graph correctly, it's not going to write it correctly to a file.Here is an alternative flat structure that will write although I don't know if this is a good way to structure the data. It makes labels that can be constructed to identify the corners of each shape.
output: