total.py (1153B)
1 import csv 2 from collections import defaultdict 3 4 input_file = "results.csv" 5 output_file = "averages.csv" 6 7 # collect durations per term 8 durations_by_term = defaultdict(list) 9 10 with open(input_file, newline="", encoding="utf-8") as f: 11 reader = csv.DictReader(f) 12 for row in reader: 13 term = row["term"] 14 try: 15 secs = int(row["duration_seconds"]) 16 durations_by_term[term].append(secs) 17 except ValueError: 18 pass 19 20 # compute averages 21 rows = [] 22 total_videos = 0 23 total_secs = 0 24 25 for term, durations in durations_by_term.items(): 26 avg = sum(durations) / len(durations) 27 total_videos += len(durations) 28 total_secs += sum(durations) 29 rows.append([term, len(durations), round(avg, 2)]) 30 31 # add overall total 32 if total_videos > 0: 33 overall_avg = total_secs / total_videos 34 rows.append(["TOTAL", total_videos, round(overall_avg, 2)]) 35 36 # write to averages.csv 37 with open(output_file, "w", newline="", encoding="utf-8") as f: 38 writer = csv.writer(f) 39 writer.writerow(["term", "video_count", "average_duration_seconds"]) 40 writer.writerows(rows) 41 42 print(f"Wrote averages to {output_file}")