2016-05-16 20 views
0

可以在this link找到該文本文件。我感興趣的是體育評分的價值。以圖形方式,它出現在Feature2 sys列下。處理文本文件以查找高於PE分數閾值3.19的值

這是我的代碼:

def main(): 

    file = open ("combined_scores.txt" , "r") 
    lines = file.readlines() 
    file.close() 

    count_pe=0 
    for line in lines: 
     line=line.strip() 

     line=line[24:31] #1problem is here:the range is not fixed in all line of the file 

     if line.find("3.19") != -1 : # I need value >=3.19 not only 3.19 
      count_pe = count_pe + 1  
    print (">=3.19: ", count_pe)#at the end i need how many times PE>3,19 occur 


main() 

回答

0

我建議你使用標籤(\ t)的解析柱中,值 「3.19」 相比較。它應該像下面這樣(Python 2.7):

with open('combined_scores.txt') as f: 
    lines = f.readlines()[1:] # remove the header line 
# reset counter 
n = 0 
for line in lines: 
    if float(line.split('\t')[-3]) >= 3.19: 
     n = n + 1 
# print total count 
print 'total=', n 
+0

無限謝謝:) –