Вот, пожалуйста:
= ^ .. ^ =
import numpy as np
import re
# collect raw data
raw_data = []
with open('data.txt', 'r') as data_file:
for item in data_file.readlines():
raw_data.append(item.strip('\n'))
data_array = np.array([])
for item in raw_data:
# remove brackets
split_data = re.split('\]', item)
for string in split_data:
# clean data
clean_data = re.sub('\[+', '', string)
clean_data = re.sub('i', 'j', clean_data)
# split data
split_data = re.split(' ', clean_data)
split_data = list(filter(None, split_data))
# handle empty list
if len(split_data) == 0:
pass
else:
# collect data
data_array = np.hstack((data_array, np.asarray(split_data).astype(np.complex)))
# reshape array
final_array = np.reshape(data_array, (int(data_array.shape[0]/12),4,3))
Выход:
[[[-0.26905 +0.956854j -0.96105 +0.319635j -0.306649+0.310259j]
[ 0.27701 -0.943866j -0.946656-0.292134j -0.334658+0.988528j]
[-0.263606-0.340042j -0.958169+0.867559j 0.349991+0.262645j]
[ 0.32736 +0.301014j 0.941918-0.953028j -0.306649+0.310259j]]
[[-0.9462 -0.932573j 0.968764+0.975044j 0.32826 -0.925997j]
[-0.306461-0.9455j -0.953932+0.892267j -0.929727-0.331934j]
[-0.958728+0.31701j -0.972654+0.309404j -0.985806-0.936901j]
[-0.312184-0.977438j -0.974281-0.350167j -0.305869+0.926815j]]
[[-0.26905 +0.956854j -0.96105 +0.319635j -0.306649+0.310259j]
[ 0.27701 -0.943866j -0.946656-0.292134j -0.334658+0.988528j]
[-0.263606-0.340042j -0.958169+0.867559j 0.349991+0.262645j]
[ 0.32736 +0.301014j 0.941918-0.953028j -0.306649+0.310259j]]
[[-0.9462 -0.932573j 0.968764+0.975044j 0.32826 -0.925997j]
[-0.306461-0.9455j -0.953932+0.892267j -0.929727-0.331934j]
[-0.958728+0.31701j -0.972654+0.309404j -0.985806-0.936901j]
[-0.312184-0.977438j -0.974281-0.350167j -0.305869+0.926815j]]]