Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I keep str(numpy.array) from automatically inserting a newline

Tags:

python

numpy

I am trying to write a 100x6 numpy array to a data file using

# print  array to file
data = open("store.dat", "w")
for j in xrange(len(array)):
    data.write(str(array[i]) + "\n")

but when the file writes, it automatically splits the line after 68 characters i.e. 5 columns of a 6 column array. For example, I want to see

[ 35.47842918  21.82382715   3.18277209   0.38992263   1.17862342   0.46170848]

as one of the lines, but am getting

[ 35.47842918  21.82382715   3.18277209   0.38992263   1.17862342
0.46170848]

I've narrowed it down to a problem with str(array[i]) and it deciding to make itself a new line.

Secondly, is there a better way to be going about this? I'm very new to python and know little about properly coding in it. Ultimately, I'm writing out a simulation of stars to later be read by a module that will render the coordinates in VPython. I thought, to avoid the problems of real-time rendering I could just pass a file once the simulation is complete. Is this inefficient? Should I be passing an object instead?

Thank you

like image 477
Robin Newhouse Avatar asked Mar 24 '23 08:03

Robin Newhouse


1 Answers

Perhaps it would be more convenient to write it using numpy.save instead?

Alternatively, you can also use:

numpy.array_str(array[i], max_line_width=1000000)
like image 191
Wolph Avatar answered Apr 21 '23 20:04

Wolph