This is similar to How to convert an array of strings to an array of floats in numpy.
I have a list of strings:
dat = [
' 1 2 1.040000e+005 0.030000\n',
' 2 7 0.000000e+000 0.030000\n',
' 3 15 0.000000e+000 0.030000\n',
]
Here are my failed attempts to make a numpy record array:
import numpy as np
dat_dtype = [
('I', 'i'),
('J', 'i'),
('val1', 'd'),
('val2', 'd'),
]
# Attempt 1
np.array(dat, dat_dtype)
# looks like garbage
# Attempt 2
np.array([x.split() for x in dat], dtype=dat_dtype)
# looks like different garbage
# Attempt 3
string_ndarray = np.array([x.split() for x in dat], dtype='|S15')
# looks good so far
string_ndarray.astype(dat_dtype)
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '1.040000e+005'
I give up. Here's the only way I can get the expected output:
dat_ndarray = np.zeros(len(dat), dat_dtype)
for i, line in enumarate(dat):
dat_ndarray[i] = tuple(line.split())
print(dat_ndarray) # [(1, 2, 104000.0, 0.03) (2, 7, 0.0, 0.03) (3, 15, 0.0, 0.03)]
Is there a more direct method to get the expected record array?