0

I have a numpy.ndarray in this form :

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

how can I get access to each element and write them in a text file?

Dani Mesejo
  • 43,691
  • 6
  • 29
  • 53
Goli A
  • 13
  • 7
  • If you would like to only write to a file, this has already been answered: https://stackoverflow.com/questions/3685265/how-to-write-a-multidimensional-array-to-a-text-file – sk_462 Jan 13 '19 at 13:52
  • That's a poor `duplicate`, dealing with a more complicated 3d case. This appears to be a simple 2d integer dtype array. `np.savetxt('test.txt', arr, fmt = ' %5d', delimiter=',')` should do the trick. See its docs for details on `fmt` and `delimiter`. – hpaulj Jan 13 '19 at 17:10
  • The reason your display has `...` is because it's summarizing a large array (>1000 elements). That threshhold can be changed, though I prefer to just display a useful slice of the array, not the whole thing. But for writing to a file, a simpler format like `csv` is better. The '[]' get in the way of reloading the text file. – hpaulj Jan 13 '19 at 17:14

0 Answers0