If I only have the string-representation of a numpy.array
:
>>> import numpy as np
>>> arr = np.random.randint(0, 10, (10, 10))
>>> print(arr) # this one!
[[9 4 7 3]
[1 6 4 2]
[6 7 6 0]
[0 5 6 7]]
How can I convert this back to a numpy array? It's not complicated to actually insert the ,
manually but I'm looking for a programmatic approach.
A simple regex replacing whitespaces with ,
actually works for single-digit integers:
>>> import re
>>> sub = re.sub('s+', ',', """[[8 6 2 4 0 2]
... [3 5 8 4 5 6]
... [4 6 3 3 0 3]]
... """)
>>> sub
'[[8,6,2,4,0,2],[3,5,8,4,5,6],[4,6,3,3,0,3]],' # the trailing "," is a bit annoying
It can be converted to an almost (dtype may be lost but that's okay) identical array:
>>> import ast
>>> np.array(ast.literal_eval(sub)[0])
array([[8, 6, 2, 4, 0, 2],
[3, 5, 8, 4, 5, 6],
[4, 6, 3, 3, 0, 3]])
But it fails for multidigit integers and floats:
>>> re.sub('s+', ',', """[[ 0. 1. 6. 9. 1. 4.]
... [ 4. 8. 2. 3. 6. 1.]]
... """)
'[[,0.,1.,6.,9.,1.,4.],[,4.,8.,2.,3.,6.,1.]],'
because these have an additional ,
at the beginning.
A solution doesn't necessarily need to be based on regex, any other approach that works for unabriged (not shortened with ...
) bool/int/float/complex arrays with 1-4 dimensions would be ok.
See Question&Answers more detail:
os