从文本文件读取n维复杂数组到numpy

时间:2019-06-17 08:01:53

标签: python python-3.x numpy text

我正在尝试将N维复杂数组从文本文件读取到numpy。文本文件的格式如下所示(包括文本文件中的方括号,一行):

[[[-0.26905+0.956854i -0.96105+0.319635i -0.306649+0.310259i] [0.27701-0.943866i -0.946656-0.292134i -0.334658+0.988528i] [-0.263606-0.340042i -0.958169+0.867559i 0.349991+0.262645i] [0.32736+0.301014i 0.941918-0.953028i -0.306649+0.310259i]] [[-0.9462-0.932573i 0.968764+0.975044i 0.32826-0.925997i] [-0.306461-0.9455i -0.953932+0.892267i -0.929727-0.331934i] [-0.958728+0.31701i -0.972654+0.309404i -0.985806-0.936901i] [-0.312184-0.977438i -0.974281-0.350167i -0.305869+0.926815i]]]

我希望将其读取为2x4x3复杂ndarray。

文件可能很大(例如2x4x10e6),因此阅读中的任何效率都会对您有所帮助。

2 个答案:

答案 0 :(得分:0)

似乎您的文件不在“ pythonic”列表中(对象之间没有逗号)。

我假设以下情况:

  1. 您无法更改您的输入,您可以从第三方来源获得它)
  2. 该文件不是csv。 (行之间没有定界符)

因此:

  1. 尝试将字符串转换为python字符串,在每个“ []”之后添加“,”-> [[1+2j, 3+4j], [1+2j, 3+4j]]
  2. 在每个数字之间添加“,”,并从“ i”更改为“ j” [-0.26905 + 0.956854j,-0.96105 + 0.319635j,-0.306649 + 0.310259j]
  3. python复数以字母j 1+2j
  4. 然后将其保存为csv。
  5. 用熊猫打开,阅读scv看一下链接:python pandas complex number

答案 1 :(得分:0)

您在这里:

= ^ .. ^ =

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]]]