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.
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.
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'])
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.
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)