Reputation: 57
I'm just starting to use Networkx and have a small problem. When I try to create a connected subgraph of my graph G it returns the error
"UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 76: ordinal not in range(128)".
I have no idea what it means.
import networkx as nx;
G = nx.read_gml(r'path.gml');
cluster= nx.average_clustering(G);
H = G.subgraph(nx.connected_components(G),key=len);
diam=nx.diameter(H);
Any kind of help is really appreciated !
Upvotes: 0
Views: 179
Reputation: 2368
This fails on read_gml
, possibly because of unicode characters within the GML file.
According to this note, the GML is supposed to be ascii
encoded with special characters appearing as HTML special characters.
There are a couple of things you can do to correct this:
When creating the graph, decode
your node labels as indicated in this question (also, please note the relevant Python Unicode guidance as well).
Create your own mapping between the characters in the node labels and those that eventually get written to the file. These can be represented as functions and the decoding function can also be used by read_gml as the destringizer
.
Both of these solutions mean that you have full control of the GML file. If you don't have full control of the GML, you can read it in as a simple file and look for patterns of the form label->"->various characters->" and then try to replace those labels, possibly with something like #2 suggested above.
Hope this helps.
Upvotes: 1