本文介绍了从文本文件中读取 n 维复数数组到 numpy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to read a N-dimensional complex array from a text file to numpy. The text file is formatted as shown below (including the square brackets in the text file, in a single line):

[[[ - 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.97704901i] -0.312184-0.977040100000000001i] -0.972654+0.309404i] -0.312184-0.97704061000000000

我希望将其读入 2x4x3 复杂的 ndarray.

I would like this to be read to a 2x4x3 complex ndarray.

文件可能非常大(比如 2x4x10e6),因此任何阅读效率都会有所帮助.

The file can be quite large (say 2x4x10e6) so any efficiency in the reading would really help out.

推荐答案

给你:

=^..^=

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

这篇关于从文本文件中读取 n 维复数数组到 numpy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 19:01