1

I am trying to read a gexf file using nx and get the above error:

import networkx as nx
nx.read_gexf('KwNet-Journals/KwNet-Journals.gexf')

The gexf file can be found here.

3 Answers 3

3

NetworkX doesn't support GEXF 1.3 that this file is in.


In [5]: pdb
Automatic pdb calling has been turned ON

In [6]: nx.read_gexf(r"<...>\KwNet-Journals.gexf")
<...>
NetworkXError: No <graph> element in GEXF file.
<...>

ipdb> l
    688         for version in self.versions:
    689             self.set_version(version)
    690             g = self.xml.find(f"{{{self.NS_GEXF}}}graph")
    691             if g is not None:
    692                 return self.make_graph(g)
--> 693         raise nx.NetworkXError("No <graph> element in GEXF file.")
    694
<...>

ipdb> self.NS_GEXF
'http://www.gexf.net/1.2draft'

ipdb> q

In [7]: ! head "<...>\KwNet-Journals.gexf"
<?xml version="1.0" encoding="UTF-8"?>
<gexf xmlns="http://www.gexf.net/1.3" version="1.3" xmlns:viz="http://www.gexf.net/1.3/viz" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.gexf.net/1.3 http://www.gexf.net/1.3/gexf.xsd">
  <meta lastmodifieddate="2020-02-07">
    <creator>Gephi 0.9</creator>
    <description></description>
  </meta>
  <graph defaultedgetype="undirected" timeformat="double" timerepresentation="timestamp" mode="dynamic">
<...>


In [10]: nx.read_gexf(r"<...>\KwNet-Journals.gexf", version='1.3')

NetworkXError: Unknown GEXF version 1.3.
> c:\python38\lib\site-packages\networkx\readwrite\gexf.py(253)set_version()
    251         d = self.versions.get(version)
    252         if d is None:
--> 253             raise nx.NetworkXError(f"Unknown GEXF version {version}.")
    254         self.NS_GEXF = d["NS_GEXF"]
    255         self.NS_VIZ = d["NS_VIZ"]

ipdb> version
'1.3'

ipdb> self.versions.keys()
dict_keys(['1.1draft', '1.2draft'])

4
  • Thanks for your answer, any ideas for reading or converting this file to use that in python?
    – keramat
    Jan 30, 2021 at 10:08
  • @keramat Add support of that version to NetworkX? If you need the graph specifically in NetworkX, I see no other way. Jan 30, 2021 at 10:11
  • @keramat You can forge the version inside the file to deceive the version checker but since it's another version of the format, it'll probably break somewhere else down the line. Jan 30, 2021 at 10:12
  • Did you find any solution to your problem? I need to import a gexf file into Networkx and I've having the same issue...
    – gccallie
    Dec 7, 2021 at 11:22
1

You might be able to get away by replacing the header of the GEXF file with namespace descriptions related to GEXF version 1.2. That is, remove

<gexf xmlns="http://www.gexf.net/1.3" version="1.3" 
xmlns:viz="http://www.gexf.net/1.3/viz" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.gexf.net/1.3 http://www.gexf.net/1.3/gexf.xsd">

and insert

<gexf version="1.2" xmlns="http://www.gexf.net/1.2draft" 
xmlns:viz="http://www.gexf.net/1.2/viz" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.gexf.net/1.2draft 
http://www.gexf.net/1.2draft/gexf.xsd">

Ideally, NetworkX is to be revised for support for GEXF that Gephi, for example, uses by default.

1

I do not take any pride in this answer as it circumvents XML syntax. However, as it gets the job done, I will share it anyway.

f = io.open("demo.gexf", mode="r", encoding="utf-8")
gexf = f.read()
gexf = gexf.replace('xmlns="http://www.gexf.net/1.3"', 'xmlns="http://www.gexf.net/1.2draft"')\
.replace('version="1.3"',  'version="1.2"')\
.replace('http://www.gexf.net/1.3', 'http://www.gexf.net/1.2')
out = io.open("demo-fixed.gexf", mode='w', encoding="utf-8")
out.write(gexf)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.