How to create a networkx node with a colon?
Question:
I need to create a graph using networkx
(and pydot2
) from data that I can’t predict. I have a problem with the :
character. If I want to create a node called I like Python because : it's fast and readable
, networkx
will create two nodes, one called I like python because : it's fast and readable
and another called it's fast and readable
. It considers the colon as a node separation operator.
How can I avoid that ? Is there a way to escape the colon, or to tell networkx
not to parse then ? The nodes are represented as strings (with the colon in them).
Answers:
Use the networkx 1.10 and everything works fine:
From a python interactive shell:
>>> import networkx as nx
>>> G=nx.Graph()
>>> G.add_node("I like Python because : it's fast and readable")
>>> G.nodes()
["I like Python because : it's fast and readable"]
As you use a quote '
inside the string use double quote "
as string delimiter.
I need to create a graph using networkx
(and pydot2
) from data that I can’t predict. I have a problem with the :
character. If I want to create a node called I like Python because : it's fast and readable
, networkx
will create two nodes, one called I like python because : it's fast and readable
and another called it's fast and readable
. It considers the colon as a node separation operator.
How can I avoid that ? Is there a way to escape the colon, or to tell networkx
not to parse then ? The nodes are represented as strings (with the colon in them).
Use the networkx 1.10 and everything works fine:
From a python interactive shell:
>>> import networkx as nx
>>> G=nx.Graph()
>>> G.add_node("I like Python because : it's fast and readable")
>>> G.nodes()
["I like Python because : it's fast and readable"]
As you use a quote '
inside the string use double quote "
as string delimiter.