Calculate year-wise total number of runs of Sachin
OneDayInternational.csv is a csv file containing about 200 records on Sachin’s ODIs from 1990 to 1998. Read this CSV file using DictReader() and calculate year-wise total number of runs of Sachin. Display the same in the following format:
1990 239
1991 417
…….
Code :-
import csv reader = csv.DictReader(open('OneDayInternational.csv','r')) prev_year = -1 run_sum = -1 for row_no, line in enumerate(reader) : year = int(line['MatchDate'].split("/")[2]) runs = int(line['Runs']) if year == prev_year: run_sum += runs else: if prev_year == -1 : prev_year = year run_sum = runs else : print(str(prev_year) + " " + str(run_sum)) prev_year = year run_sum = runs print(str(prev_year) + " " + str(run_sum))