I have a csv dataset like this:
A, 10, USA
B,30, UK
C,4,IT
A,20,UK
B,10,USA
I want to read this csv lines and provide the following output:
A has ran 30 miles with average of 15.
B has ran 30 miles with average of 20.
C has ran 4 miles with average of 4.
My solution so far is to read the csv data and convert them to dictionary and then Iterate over them to see how many times 'A' has been repeated and what values it has had to calculate for the average and finally producing the result. I have already written this code, but I have a hard time to efficiently calculate for the number of times that A has been repeated and add up the miles to create my final output. any thoughts to do this in Python? It is sort of easy for me to do this in C#, but I am not that good with Python.
def main(filename):
f = open(filename,'r')
labels = ['name','miles','country']
data = csv.DictReader(f,labels,delimiter=',')
for line in data:
print (line['name']+" " + "has ran" +" "+ line['miles']+" "+"miles")
f.close()