繁体   English   中英

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

[英]Reading an n-dimensional complex array from a text file to numpy

我试图从文本文件中读取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),因此读取的任何效率都会有所帮助。

因为看起来你的文件不在“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. 用pandas打开,看看scv看链接: python pandas复数

干得好:

= ^ .. ^ =

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM