在python中制作一个2D数组

时间:2012-10-31 15:52:45

标签: python list file

我的任务是编写一个2D数组,这将允许我重复循环该行并将单元格存储在5长度数组中。我想要帮助的是如何创建它以便它保持循环直到它达到最后5个值并存储它们。

举个例子,我的.csv文件中有6个整行

line = "1,9/20/2012, 48.019,34.888,37.334,35.425,36.69,38.916,36.837,39.212,37.528,37.404"
line = "1,9/20/2012, 38.019,54.888,36.334,35.425,36.69,38.916,36.837,39.212,37.528,37.404"
line = "1,9/20/2012, 28.019,31.888,56.334,33.825,36.69,38.916,36.837,39.212,37.528,37.404"
line = "1,9/20/2012, 48.019,34.888,37.334,35.425,36.69,38.916,36.837,39.212,37.528,37.404"
line = "1,9/20/2012, 38.019,54.888,31.334,37.425,33.69,38.916,36.837,39.212,37.528,37.404"
line = "1,9/20/2012, 28.019,31.888,56.334,33.825,36.69,38.916,36.837,39.212,37.528,37.404"

我已将脚本设置为跳过前2个值[1,9 / 20/2012]

然后我将它们分开,这意味着前5个值为htr1,第2个htr2 [ 48.019,34.888,37.334,35.425,36.69] [38.916,36.837,39.212,37.528,37.404]

基本上我需要帮助的是接收列中的最后5个值并将它们存储在python中的数组或列表中。例如:

htrA[38.019,28.019,48.019,38.019,28.019]
htrB[36.334,56.334,37.334,31.334, 56.334

这是我到目前为止的代码

inFile = open("input_test.csv", "r")
outFile = open("results.txt", "w")

#To reliably determine steady state temperature average fifoSize last temperature readings
fifoSize = 5        #last fifoSize to average to get final temperature
bufFifo = FiFoBuf(fifoSize) 

#Write Header
#outFile.write('Test Name,X+ avg,X+ std,X+ count,X- avg,X- std,X- count,X angle,Y+ avg,Y+ std,Y+ count,Y- avg,Y- std,Y- count,Y angle\n')

for line in inFile:

    print line
    #Characters of each line as list - items that were separated by commas
    list = line.rstrip().replace(' ','').split(',')
    list = list[2:]     #remove index and date code (1st 2 items of list)

    htr1 = list[0:5]    #1st heater temperatures
    htr2 = list[6:10]   #2nd heater temperatures



    print "\nhtr1: "
    print htr1
    print "\nchDeviation(htr1): "
    print chDeviation(htr1)

    avg()
#printStats()

inFile.close()
outFile.close()

4 个答案:

答案 0 :(得分:0)

由于您只需要最后5行,因此可以使用unix命令tail -n 5来获取最后5行。然后你可以简单地阅读每一行并根据需要追加。

如果那是不可能的(因为你不能使用unix命令)你可以在python中创建一个天真的版本,如下所示:

for line in file.readlines()[-5:]:
     #do whatever appending you need to do     

如果文件非常大,您可以向后读取文件的末尾,直到您阅读了五个换行符,然后拆分换行符。有这方面的食谱。

答案 1 :(得分:0)

为每列创建一个新列表columnX=[],然后为每一行调用columnX.append(item)以收集所有第X个元素。

column0.append(line[0])
column1.append(line[1])
#...

答案 2 :(得分:0)

你可以把你所有的行放在一个列表中,从中取出最后5个元素(最后5行),然后将行分成逗号周围的圆圈,获得Hans Then建议的列表列表。您从虚假空格中剥离值,然后使用zip函数的一个小魔术来转置行。您获取列表列表,但每个列表对应于您的一个列

lines1 = [ line1, line2, line3, line4, line5, line6 ]
lines2 = [ [s.strip() for s in l.split(',')[2:]] for l in lines2 ][-5:]
lines3 = zip(*lines2)

print lines3
#[('38.019', '28.019', '48.019', '38.019', '28.019'),
# ('54.888', '31.888', '34.888', '54.888', '31.888'),
# ('36.334', '56.334', '37.334', '31.334', '56.334'),
# ('35.425', '33.825', '35.425', '37.425', '33.825'),
# ('36.69', '36.69', '36.69', '33.69', '36.69'),
# ('38.916', '38.916', '38.916', '38.916', '38.916'),
# ('36.837', '36.837', '36.837', '36.837', '36.837'),
# ('39.212', '39.212', '39.212', '39.212', '39.212'),
# ('37.528', '37.528', '37.528', '37.528', '37.528'),
# ('37.404', '37.404', '37.404', '37.404', '37.404')]

答案 3 :(得分:0)

这是我的答案的修订版本,它不使用csv模块,而是读取并解析文件本身的每一行。

htrA = []
htrB = []
with open("input_test.csv", "rt") as inFile:
    for line in inFile:
        line = [float(value) for value in line.split(',')[2:]] # skips first 2 cols
        htr1 = line[0:5]
        htr2 = line[5:10]
        htrA.append(htr1[0])
        htrB.append(htr1[1])

htr2d = [htrA[-5:], htrB[-5:]]  # want just the last 5 rows
print 'htr2d:'
for row in htr2d:
    print '  ', row

输出:

htr2d:
   [38.019, 28.019, 48.019, 38.019, 28.019]
   [54.888, 31.888, 34.888, 54.888, 31.888]

您可以使用htr2d[row][column]访问htr2d的各个元素 例如:

print htr2d[0][3]  # 38.019