Skip to content Skip to sidebar Skip to footer

Undo Pretty Print From Numpy

As an example: from numpy import * A = array([2/3., 4]) print A Gives [ 0.66666667 4. ] How do I take the string: S = '[ 0.66666667 4. ]' And convert S back into

Solution 1:

You need fromstring(S, sep=' '). You almost parsed the string as packed binary data instead of printable floats. The string should only contain numbers and the delimiter (space), so trim it to get rid of the brackets. E.g., S[1:-1].

You probably know this but let's spell it out: This may be handy for recovering the results of a computation (or recovering from a mistake), but it is not a good way to store and reload data. It is inconvenient and you lose precision in the conversion. Your functions should return an array instead of printing it out, and if you need to store data for use by a later program run, use a digital storage format (e.g., see the cPickle module or whatever numpy provides for the purpose-- note that fromstring can also read packed binary data)

Post a Comment for "Undo Pretty Print From Numpy"