So I have a text file with multiple lines Each line has the name, grade, and birthyear or a student, seperated by semi colons
How do I make a function so that it sums all of the second items in each line, and then averages them?
for example,
mary; 0; 1995
jay; 50; 1995
classAverage = 25
Really confused with this.
Here is my code so far, it doesn't give me errors, but when I print it says <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)
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=';')]))
I get an error when I run that code, but you would get that output if you had "print classAverage" instead of "print classAverage(data)", so maybe you copied a slightly different version than what produced that output.
You have several problems in your code. The first is that data is a list and you are trying to call data.split(). You also never split the text by ";" and your average formula is off. I made some slight adjustments to get it to do what I think you intend:
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)
That said, pandas is really good at parsing data files and then calculating metrics on the data. Parsing the file is a single line using pandas. Here is the equivalent code using pandas:
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()
Output:
Average score = 25.0
Number born from 1995 to 1997 is 2
you should modify function classAverage like this:
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.