Unexpected error reading GML graph

I uploaded a gml file containing the dolphins social network.

Some time ago I did some analysis on this network running python 3.4 and networkx 1.9 on a Windows7 machine, but now I run on a Linux machine (with the same version of python, but with networkx 1.10 ) and found a problem while trying to read the file .

This is the code used to read the file:

import networkx as nx nx.read_gml("dolphins.gml") 

And this is the error stack trace:

 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<string>", line 2, in read_gml File "/usr/lib/python3.4/site-packages/networkx/utils/decorators.py",line 220, in _open_file result = func(*new_args, **kwargs) File "/usr/lib/python3.4/site-packages/networkx/readwrite/gml.py", line 210, in read_gml G = parse_gml_lines(filter_lines(path), label, destringizer) File "/usr/lib/python3.4/site-packages/networkx/readwrite/gml.py", line 383, in parse_gml_lines graph = parse_graph() File "/usr/lib/python3.4/site-packages/networkx/readwrite/gml.py", line 372, in parse_graph curr_token, dct = parse_kv(next(tokens)) File "/usr/lib/python3.4/site-packages/networkx/readwrite/gml.py", line 323, in tokenize (line[pos:], lineno + 1, pos + 1)) networkx.exception.NetworkXError: cannot tokenize 'graph' at (1, 1) 

Can you read the file? Has anyone experienced a close problem? or knows what generates an error?

Thank you in advance!

+6
source share
2 answers

It worked by downgrading the networkx version from 1.10 to 1.9.1.

Hope this answer can help someone else.

+2
source

In newer versions of networkx, the gml file should follow a more specific format. The problem with dolphins.gml is that there should be no carriage return before open square brackets. For instance:

Wrong format:

 graph [ directed 0 node [ id 0 label "Beak" ] . . . 

The correct format is:

 graph [ directed 0 node [ id 0 label "Beak" ] . . . 

It doesn't matter how many spaces there are in front of the square bracket, if there are more than one, and carriage return is not possible.

As a result, I used a regular expression to get rid of the white spaces in front of the square brackets of the opening. The following regular expression worked for me:

 \s+\[ 

and just replace it with "[". There must be at least one space in front of the bracket.

Also keep in mind that each node must have a unique label.

Hope this helps.

+5
source

All Articles