2015-09-21 67 views
1

所以我有一個多行 一個文本文件,每一行都有名稱,等級,以及birthyear或學生,通過半冒號拆分列表是在一個文件

分隔怎樣使一個功能,以便它總結每一行中的所有第二項,然後對它們進行平均?

例如,

mary; 0; 1995 
jay; 50; 1995 

classAverage = 25 

與此真的混淆。

這裏是我的代碼,到目前爲止,它並沒有給我的錯誤,但是當我打印出來說<function classAverage at 0x0000000004C1ADD8>

from kiva.constants import LINES 

def process(name): 
    f = open(name) 
    answer = [] 
    for line in f: 
     answer.append(line.strip()) 
    return answer 
def classAverage(data): 
    data = process(filename) 
    data.split() 
    adding = [] 
    for line in data: 
     adding = adding + data[1] 
    return adding/(line) 


if __name__ == '__main__': 
    filename = "grades.txt" 
    data = process(filename) 
    for each in data: 
     print each 
    print classAverage(data) 
    #print "Average grade is ", classAverage(data) 
    year1 = 1995 
    year2 = 1997 
    print "Number born from ",year1,"to",year2,"is", 
    #print howManyInRange(data, year1, year2) 

回答

1
def ave(x): 
    return sum(x)/len(x) 
with open(name, newline='') as csvfile: 
    print(ave([float(row[1]) for row in csv.reader(csvfile, dilimeter=';')])) 
0

當我運行的代碼我得到一個錯誤,但你會得到如果您輸出了「print classAverage」而不是「print classAverage(data)」,那麼您可能複製的版本與生成該輸出的版本略有不同。

在代碼中有幾個問題。首先是數據是一個列表,你正在嘗試調用data.split()。你也永遠不會按「;」分割文本和你的平均公式關閉。我做了一些輕微的調整,以得到它做什麼,我想你打算:

def process(name): 
f = open(name) 
answer = [] 
for line in f: 
    answer.append(line.strip().split(';')) 
return answer 


def classAverage(data): 
    adding = 0.0 
    for line in data: 
     adding = adding + float(line[1]) 
    return adding/len(data) 


if __name__ == '__main__': 
    filename = "grades.txt" 
    data = process(filename) 
    for each in data: 
     print each 
    print classAverage(data) 
    # print "Average grade is ", classAverage(data) 
    year1 = 1995 
    year2 = 1997 
    print "Number born from ", year1, "to", year2, "is", 
    # print howManyInRange(data, year1, year2) 

也就是說,熊貓是在解析數據文件,然後計算的數據指標非常好。解析文件是一個使用熊貓的單行。下面是對應的使用代碼大熊貓:

import pandas as pd 


if __name__ == '__main__': 
    df = pd.read_table('grades.txt', sep=';', names=['name', 'score', 'year']) 
    print 'Average score = ', df.score.mean() 
    year1 = 1995 
    year2 = 1997 
    print "Number born from ", year1, "to", year2, "is", df[(df.year >= year1) & (df.year <= year2)].name.count() 

輸出:

Average score = 25.0 
Number born from 1995 to 1997 is 2 
0

你應該修改功能classAverage這樣的:

def classAverage(data): 
    # you do not need to re-process the file, just use the data 
    adding = [] 
    for line in data: 
     line = line.split(';') 
     adding.append(float(line[1].strip())) 
    return sum(adding)/len(adding)