2012-10-31 51 views
0

我的任務是編寫一個2D數組,它將允許我重複循環遍歷該行並將這些單元格存儲在一個5長度的數組中。我想要幫助的是如何創建它,以便它保持循環,直到達到最後5個值並存儲它們。在python中製作二維數組

因此,作爲一個例子,我在我的.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中使用rray或list。例如:

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() 
+0

所以這不起作用? –

+0

不需要工作 – user1778360

+0

您爲'htrB'顯示的值不是來自下一列,而是來自輸入數據之外的值。 – martineau

回答

0

既然你只需要在最後5行,你可以使用Unix命令tail -n 5只拿到最後5行。然後,您可以簡單地閱讀每行並根據需要追加。

如果這是不可能的(因爲你不能使用UNIX命令),你可以像這樣在Python中創建一個天真的版本:

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

如果文件是真的大,可以從的讀取結束向後文件,直到你閱讀了5條換行符,然後拆分換行符。有這方面的食譜。

+0

很好,但基本上,我的.csv文件有很多行,「我已經向你顯示了6行」,我希望python遍歷每一行並存儲類似於htrA [上面所述]的內容,它是每個行的最後5位數柱。 – user1778360

0

爲每列創建一個新列表columnX=[],然後調用columnX.append(item)爲每行收集所有第X個元素。

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

你可以把你所有的線列表中的,採取從它的最後5個元素(最後5行),然後將行分成圍繞逗號piecies,獲取列表的列表中的建議漢斯那麼。你從虛假的空白中刪除值,然後使用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')] 
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 
+0

這是錯誤我得到 回溯(最後最近一次調用): 文件「C:\ script_orginal ......」,線路135 爲讀者在線: _csv.error:行包含空字節 – user1778360

+0

由於某種原因,你的.csv文件中有一個NULL字節(它們應該是純文本文件)。在'open()'調用中嘗試使用''rt''而不是''rb''。 – martineau

+0

仍然給我同樣的錯誤。該文件大小爲17kb – user1778360