Ask Your Question
1

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

asked 2021-10-21 20:15:01 +0200

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?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-10-21 21:13:22 +0200

tmonteil gravatar image

updated 2021-10-22 13:43:32 +0200

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)
edit flag offensive delete link more

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 ( 2021-10-22 10:58:49 +0200 )edit

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: 2021-10-21 20:10:26 +0200

Seen: 1,557 times

Last updated: Oct 22 '21