14
votes

I can convert a string representation of a list to a list with ast.literal_eval. Is there an equivalent for a numpy array?

x = arange(4)
xs = str(x)
xs
'[0 1 2 3]'
# how do I convert xs back to an array

Using ast.literal_eval(xs) raises a SyntaxError. I can do the string parsing if I need to, but I thought there might be a better solution.

2
The numpy array doesn't provide a repr that can be used to reconstruct even a python list. You could doctor the string to recreate a list then create a numpy array from that e.g. numpy.array(ast.literal_eval(', '.join(xs.split(' ')))) - Paul Rooney
Is it essential that you use ast.literal_eval? If so, then the answer is no, you can't get a numpy array from literal_eval. From the python documentation of ast.literal_eval(node_or_string): "The string or node provided may only consist of the following Python literal structures: strings, numbers, tuples, lists, dicts, booleans, and None." If what you really want is a convenient way to convert a numpy array to a string and then back to an array, please elaborate on that in the question. - Warren Weckesser
Sorry for not being clearer. I was curious of there was an analog for ast.literal_eval that worked for numpy arrays, but didn't expect to use ast.literal_eval. - jdmcbr

2 Answers

14
votes

For 1D arrays, Numpy has a function called fromstring, so it can be done very efficiently without extra libraries.

Briefly you can parse your string like this:

s = '[0 1 2 3]'
a = np.fromstring(s[1:-1], dtype=np.int, sep=' ')
print(a) # [0 1 2 3]

For nD arrays, one can use .replace() to remove the brackets and .reshape() to reshape to desired shape, or use Merlin's solution.

15
votes

Try this:

xs = '[0 1 2 3]'

import re, ast
ls = re.sub('\s+', ',', xs)
a = np.array(ast.literal_eval(ls))
a  # -> array([0, 1, 2, 3])