Perhaps you could use re.finditer
with np.fromiter
:
import re
import numpy as np
text = '(1,2),(3,4),'
arr = np.fromiter((item.group()
for item in
re.finditer(r'\d+', text)),
dtype=np.int64).reshape((-1, 2))
print(arr)
yields
array([[1, 2],
[3, 4]], dtype=int64)
Note that if you could arrange for the text to be:
'1 2\n3 4'
Then it could be loaded into a NumPy array much more easily (and faster):
import io
text = '1 2\n3 4'
np.genfromtxt(io.BytesIO(text), dtype=np.int64)
yields
array([[1, 2],
[3, 4]], dtype=int64)