The straightforward solution you have already mentioned is to strip and split the input string using the []
brackets, create a nested list and turn it into a NumPy array:
s = '[[0123][1234][3456]]'
t = (
s
.strip('][') # '0123][1234][3456'
.split('][') # ['0123', '1234', '3456']
)
a = np.array([[int(v) for v in u] for u in t])
You could use np.fromiter
to replace the inner list comprehension:
a = np.array([np.fromiter(u, dtype=int) for u in t])
Using np.stack
to combine the inner 1-d arrays works as well:
a = np.stack([np.fromiter(u, dtype=int) for u in t])
Similarly to Reconcile np.fromiter and multidimensional arrays in Python, you could first use np.fromiter
to create a 1-d array containing all the numbers and afterwards reshape it into a 2-d array:
from itertools import chain
a = np.fromiter(chain.from_iterable(t), dtype=int).reshape(len(t), -1)
But I'm not sure if there would be any benefit of doing that.