[Numpy-discussion] array 2 string

Michael McNeil Forbes mforbes at physics.ubc.ca
Wed Mar 11 10:28:14 EDT 2009


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.





More information about the NumPy-Discussion mailing list