简体   繁体   English

从文本文件中读取多维数组并将数据存储在多维数组中

[英]Reading multi-dimensional arrays from text file and storing the data in multi-dimensional arrays

I am trying to read multi-dimensional arrays from file HD.txt and store the data in arrays for computation. 我正在尝试从文件HD.txt中读取多维数组,并将数据存储在数组中以进行计算。 HD.txt looks like the following: HD.txt如下所示:

 [[[27],[0],[0],[0],[0],[0],[0]],
 [[0],[0],[0],[0],[0],[0],[0]],
 [[0],[0],[0],[0],[0],[0],[0]]]

 [[102],[0],[0],[0],[0],[0]] 

I would like to read both of these arrays and store it in two separate arrays. 我想读取这两个数组并将其存储在两个单独的数组中。 For eg the first array in HD.txt is a 3 dimensional integer array (3X7X1 array) and the second array is a 2d integer array. 例如,HD.txt中的第一个数组是3维整数数组(3X7X1数组),第二个数组是2d整数数组。 I would like to read them as 3d and 2d arrays respectively. 我想分别将它们读取为3d和2d数组。 I am not an expert in python and below is my attempt to read the first array. 我不是python专家,下面是我尝试读取第一个数组的尝试。 Needless to say it didnt work out as I wanted. 不用说它没有按我的意愿进行。 Any help in this matter is greatly appreciated. 非常感谢在此问题上的任何帮助。 Thanks 谢谢

with open("HD.txt", "r") as f:
for line in f.readlines():
   S = line.split(' ')

When using line.split() you need a character that is in between each of the values. 使用line.split() ,需要在每个值之间插入一个字符。 Since HD.txt has commas in between each value you can simply remove each [ and ] character then call split() . 由于HD.txt在每个值之间都有逗号,因此您可以简单地删除每个[]字符,然后调用split() Below is an example. 下面是一个例子。

arrayOne = []
arrayTwo = []
next = False

with open("HD.txt", "r") as f:
    for line in f.readlines():
        if line[0] == '\n':
           next = True  
        else:          
            line = line.replace("[", "")
            line = line.replace("]", "")
            line = line.replace("\n", "")

            for i in line.split(','):
                if next:
                    arrayOne.append(i)
                else:
                    arrayTwo.append(i)

If the line is blank it means that the first character will be \\n and you can start collecting the second array. 如果该行为空,则表示第一个字符为\\n ,您可以开始收集第二个数组。 Make sure to also remove the new line characters. 确保还删除换行符。

As you have control on the data generation, I advice you to remove the brackets and simply display data separated by spaces (for columns) and returns (for rows). 当您控制数据生成时,我建议您去掉括号,然后简单地显示用空格(对于列)和返回(对于行)分隔的数据。 It would lead you to this content for HD.txt : 它将带您到HD.txt此内容:

27 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0

102 0 0 0 0 0

Then to reconstruct your array, use the split() function from the shlex package ( https://docs.python.org/2/library/shlex.html ). 然后,要重建数组,请使用shlex包( https://docs.python.org/2/library/shlex.html )中的split()函数。 It separates stuff out of spaces. 它将内容分隔开。 But, you have to convert each item to an int afterwards in your case. 但是,在这种情况下,您必须随后将每个项目转换为一个int Which leads us to : 这导致我们:

import shlex

array = []

with open("HD.txt", "r") as f:
 i = 0
 for line in f.readlines():
  array[i] = shlex.split(line)
  i += 1

Don't forget to cast the integer type for each value after that with int() 不要忘记使用int()为每个值强制转换整数类型

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

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