0

I want to save the output of my Python program into a text file to use it in C for some reasons, I don't know how should I do it. The code is:

import networkx as nx
import numpy as np

t_start=0;t_end=1;dt=0.1
tpoints=np.arange(t_start,t_end,dt)
G = nx.grid_2d_graph(20,20, periodic=False, create_using=None)
adj_matrix=nx.adjacency_matrix(G)
print(adj_matrix.todense())

If the number of nodes were less than 20 (like 10 or lower) the output would be:

[[0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0]
 [1 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0]
 [0 1 0 1 0 0 1 0 0 0 0 0 0 0 0 0]
 [0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0]
 [1 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0]
 [0 1 0 0 1 0 1 0 0 1 0 0 0 0 0 0]
 [0 0 1 0 0 1 0 1 0 0 1 0 0 0 0 0]
 [0 0 0 1 0 0 1 0 0 0 0 1 0 0 0 0]
 [0 0 0 0 1 0 0 0 0 1 0 0 1 0 0 0]
 [0 0 0 0 0 1 0 0 1 0 1 0 0 1 0 0]
 [0 0 0 0 0 0 1 0 0 1 0 1 0 0 1 0]
 [0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 1]
 [0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0]
 [0 0 0 0 0 0 0 0 0 1 0 0 1 0 1 0]
 [0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 1]
 [0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0]]

But when the number of nodes increase, the output is like this:

[[0 1 0 ... 0 0 0]
 [1 0 1 ... 0 0 0]
 [0 1 0 ... 0 0 0]
 ...
 [0 0 0 ... 0 1 0]
 [0 0 0 ... 1 0 1]
 [0 0 0 ... 0 1 0]]

And so I can't copy it manually to a text file. So I need a command to write this matrix completely to a text file. Thanks for your answers.

Benyamin Jafari
  • 27,880
  • 26
  • 135
  • 150

1 Answers1

0

The displayed output of a numpy array is not meant to display everything.

If you want to save the array to a file, you can use tofile. There you can define if you want a binary or a text file.

Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62