2016-05-16 61 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

无限谢谢:) –