在python中将数据文件读入多维数组

时间:2015-11-06 01:01:04

标签: python arrays multidimensional-array

我是python的新手。我想从文件中读取数据并将其存储在多维数组中。例如,我有这个,

6 5.9
6.3 5.9
6.6 6.3

7.8 7.5
7.8 7.3
7.5 7.6

8.3 8
8.5 8
8.2 8.3

9.2 8.5
9 8.5
9.2 8.9

我想将它存储在这样的数组中:

[ [['6', '5.9'], ['6.3', '5.9'], ['6.6', '6.3']], 
  [['7.8', '7.5'], ['7.8', '7.3'], ['7.5', '7.6']], 
      [['8.3', '8'], ['8.5', '8'], ['8.2', '8.3']], 
      [['9.2', '8.5'], ['9', '8.5'], ['9.2', '8.9']] ]

到目前为止我已尝试过这个:

with open("number.txt") as textFile:
    lines = [line.split() for line in textFile]
print(lines)

它给了我这样的话:

[['6', '5.9'], ['6.3', '5.9'], ['6.6', '6.3'], [], ['7.8', '7.5'], ['7.8', '7.3'], ['7.5', '7.6'], [], ['8.3', '8'], ['8.5', '8'], ['8.2', '8.3'], [], ['9.2', '8.5'], ['9', '8.5'], ['9.2', '8.9']]

3 个答案:

答案 0 :(得分:1)

首先,您一次从文件1行读取数据。然后,对于每个空行,您将启动一个新数组。否则,您将当前数组拆分为spaces并将其附加到返回值。

您可以使用此代码:

ret = []                                 #return value
with open('data.txt', 'r') as f:         #open file
    curArray = []                        #start current array
    for line in f:                       #loop through the lines
        line = line.rstrip()             #get rid of \n
        if len(line) == 0:               #if its an empty line
            ret.append(curArray)         #  append curArray to ret
            curArray = []                #  reset curArray
        else:
            numbers = line.split(' ')    #split array on spaces
            curArray.append(numbers)     #append new array to the curArray
print ret

此代码假定每一行都应该是一个数组,每次有一个空行(只有一个换行符)时,就会启动一个新数组。

要获取所有数组中列的总和,请编写一个函数,该函数接受数组和要求和的列的索引:

def sumColumn(arr3d, index):
    sum = 0
    for arr in arr3d:
        for arr2 in arr:
            sum+=float(arr2[index])
    return sum

#now print the sum of the first column using the initial data file.
print sumColumn(ret, 0)    # the columns are 0 indexed, so 0 is the first column

答案 1 :(得分:1)

以下代码将为您提供所需的结果:

import re

dim3 = []
dim2 = []
f = open ('inputfile.txt', 'r')
for s in f.readlines():
    s = s.strip()
    if s == '':
        dim3.append(dim2)
        dim2 = []
    else:
        dim1 = re.split('\s+', s)
        dim2.append(dim1)
if len(dim2) > 0:
    dim3.append(dim2)
f.close()

print(dim3)

它基本上维护dim2/3维度变量来保存值,并使用dim1维度变量来处理每一行。

对于每个非空行,我们计算数组dim1并将其添加到当前dim2。如果我们找到一个空行,我们会将当前dim2添加到dim3并重置dim2

最后,我们处理剩余的dim2,结果dim3是您想要的多维数组(为便于阅读而格式化):

[[['6'  , '5.9'], ['6.3', '5.9'], ['6.6', '6.3']],
 [['7.8', '7.5'], ['7.8', '7.3'], ['7.5', '7.6']],
 [['8.3', '8'  ], ['8.5', '8'  ], ['8.2', '8.3']],
 [['9.2', '8.5'], ['9'  , '8.5'], ['9.2', '8.9']]]

代码是这样的,它可以处理任意维度大小,因为它允许任意数量的每行数,每组行数和每个文件组。

答案 2 :(得分:0)

这假设您的数组将是您指定的大小。这仅仅是为了演示您将需要用来解决此问题的逻辑。

matrix = []
tempLine = []

i = 0
for line in file
    if i < 3:
        #assuming you can parse those values on the line
        tempLine.append([firstValue, secondValue])
        i += 1
    if i >= 3:
        i = 0
        matrix.append(tempLine)
        tempLine = []

print matrix