2015-11-06 40 views
1

我是python的新手。我想从文件读取数据并将其存储在多维数组中。例如,我有这个,将数据文件读入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']] 
+1

您可能想要指定它是否总是每行两个元素,每组三行,每个文件四组等等,或者是否希望它比这更具动态性。 – paxdiablo

+4

所以..你到目前为止尝试过什么? –

+0

@paxdiablo,实际上,这是双向方差分析。 –

回答

1

首先你读了t他每次从文件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 
+0

我如何获得每列的总和?例子是数据文件的垂直加法。 –

+0

此外,在这种情况下,还有一点含糊不清......你是希望整个第一列的总和还是你想每个换行符的小计? – ajon

+0

我想整个第一列的总和.. –

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 
0

下面的代码会给你你想要的结果:

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

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