First time here? Check out the FAQ!

Ask Your Question
1

How to save a networkx graph so it can be read in python

asked 3 years ago

lesshaste gravatar image

Take this MWE that is to be run in sage:

import networkx as nx
G = nx.DiGraph()                                                          
G.add_edge(0,1)                                                           
G.nodes[0]['weight'] = 23                                                 
nx.write_gml(G, "test.gml")

This gives me a long list of errors ending with:

NetworkXError: 0 is not a string

The same code works without error in Python.

What can I do?

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
1

answered 3 years ago

tmonteil gravatar image

updated 3 years ago

The problem comes from the fact that 0,1,23 are preparsedas Sage Integers, not Python ints, see https://doc.sagemath.org/html/en/refe...

To work around the issue, you can either:

  • replace 0,1,23 with 0r,1r,23r (the letter r stands for "raw")
  • turn the preparser off, with the command preparser(False)
Preview: (hide)
link

Comments

We have long term open tickets for simplifying loading/saving graphs from/to files in various formats (#9731, #19477), possibly without using relying on networkx. We should work on these tickets.

David Coudert gravatar imageDavid Coudert ( 3 years ago )

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 3 years ago

Seen: 2,477 times

Last updated: Oct 22 '21