0

我的任务是编写一个 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和第二个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

4 回答 4

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 的各个元素htr2d[row][column]
例如:

print htr2d[0][3]  # 38.019
于 2012-10-31T16:38:06.467 回答
0

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

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

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

如果文件真的很大,您可以从文件末尾向后读取,直到您读取了五个换行符,然后拆分换行符。有食谱。

于 2012-10-31T16:19:53.453 回答
0

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

column0.append(line[0])
column1.append(line[1])
#...
于 2012-10-31T16:24:18.303 回答
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')]
于 2012-10-31T16:24:49.750 回答