data_process.py (1007B)
1 # 2018 David DiPaola 2 # licensed under CC0 (public domain, see https://creativecommons.org/publicdomain/zero/1.0/) 3 4 import csv 5 6 file = open('data.txt') 7 file_reader = csv.reader(file, delimiter='\t') 8 file_data = {} 9 first = True 10 for row in file_reader: 11 if first: 12 first = False 13 continue 14 row_year = row[1].strip() 15 row_month = row[2].strip() 16 row_value = float(row[3].strip()) 17 if row_year not in file_data: 18 file_data[row_year] = {} 19 file_data[row_year][row_month] = row_value 20 21 data = {} 22 for year in file_data.keys(): 23 sum = 0.0 24 count = 0 25 for month in file_data[year].keys(): 26 sum += file_data[year][month] 27 count += 1 28 data[year] = sum/count 29 30 data_years = sorted(data.keys()) 31 print('const int DATA_STARTYEAR = %s;' % data_years[0]) 32 print('const int DATA_ENDYEAR = %d;' % (int(data_years[0]) + len(data_years) - 1)) 33 print('') 34 print('const float DATA[] = {') 35 for year in data_years: 36 print('\t' + '%ff,' % data[year]) 37 print('};') 38