def get_space_separated_data(raw):
assert raw[0].strip().endswith("= [")
assert raw[-1].strip().endswith("];")
header = raw[0].replace("= [", "").strip()
shape = tuple(eval(header[header.index("["):header.index("]") + 1]))
data = [eval(line.strip().replace(" ", ",")) for line in raw[1:-1]]
if len(shape) == 1:
step_size = 1
else:
step_size = functools.reduce(operator.mul, shape[:-1])
years = np.array(data[::step_size + 1], dtype=int)
subarrays = [
np.array(data[index * (step_size + 1) + 1:(index + 1) * (step_size + 1)]).reshape(shape)
for index in range(len(years))
]
return header, years, np.stack(subarrays, axis=-1)
评论列表
文章目录