Hello, I want to convert an array to a string. I like array2string, but it puts these annoying square brackets around the array, like [[1 2 3], [3 4 5]] Anyway we can suppress the square brackets and get (this is what is written with savetxt, but I cannot get it to store in a variable) 1 2 3 4 5 6 Thanks, Mark
On Tue, 10 Mar 2009 17:21:23 +0100, Mark Bakker wrote:
Hello,
I want to convert an array to a string.
I like array2string, but it puts these annoying square brackets around the array, like
[[1 2 3], [3 4 5]]
Anyway we can suppress the square brackets and get (this is what is written with savetxt, but I cannot get it to store in a variable) 1 2 3 4 5 6
This isn't pretty, but: out = '' for i in range( 0 , 2 ): for j in range( 0, 3 ): out += str( A[i,j] ) + ' ' out += '\n' print out
On 10 Mar 2009, at 10:33 AM, Michael S. Gilbert wrote:
On Tue, 10 Mar 2009 17:21:23 +0100, Mark Bakker wrote:
Hello,
I want to convert an array to a string.
I like array2string, but it puts these annoying square brackets around the array, like
[[1 2 3], [3 4 5]]
Anyway we can suppress the square brackets and get (this is what is written with savetxt, but I cannot get it to store in a variable) 1 2 3 4 5 6
How about using StringIO:
a = np.array([[1,2,3],[4,5,6]]) f = StringIO() savetxt(f, a, fmt="%i") s = f.getvalue() f.close() print s 1 2 3 4 5 6
Michael.
a = array(....)
b = str(a).replace('[','').replace(']','')
there's probably a better way, but it works.
On Tue, Mar 10, 2009 at 12:21 PM, Mark Bakker
Hello,
I want to convert an array to a string.
I like array2string, but it puts these annoying square brackets around the array, like
[[1 2 3], [3 4 5]]
Anyway we can suppress the square brackets and get (this is what is written with savetxt, but I cannot get it to store in a variable) 1 2 3 4 5 6
Thanks, Mark _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
On Tue, Mar 10, 2009 at 11:37 AM, Pierre GM
Simplifying the loops from a previous poster: >>> "\n".join((" ".join((str(_) for _ in x)) for x in a)) - Show quoted text -
or if you want to control the formatting, e.g. print "\n".join(("%-10.6f "*a.shape[1] % tuple(x) for x in a)) or print "\n".join(("%6d"*a.shape[1] % tuple(x) for x in a))
participants (6)
-
Chris Colbert
-
josef.pktd@gmail.com
-
Mark Bakker
-
Michael McNeil Forbes
-
Michael S. Gilbert
-
Pierre GM