Read printed numpy array
The other reply works great, but some shortcuts can be taken if the values are numeric. Furthermore, you may have an array with many dimension and even many orders. Given npstr
, your str(np.array):
import re, jsonimport numpy as np# 1. replace those spaces and newlines with commas.# the regex could be '\s+', but numpy does not add spaces.t1 = re.sub('\s',',',npstr)# 2. covert to listt2 = json.loads(t1)# 3. convert to arraya = np.array(t2)
In a single line (bad form sure, but good for copypasting):
a = np.array(json.loads(re.sub('\s',',',npstr)))