update lecture material to move to notebook
[harrypotter-wikipedia-cdsw] / hpwp-trend.py
1 import encoding_fix
2
3 from csv import DictReader
4
5 # read in the input file and count by day
6 input_file = open("hp_wiki.tsv", 'r', encoding="utf-8")
7
8 edits_by_day = {}
9 for row in DictReader(input_file, delimiter="\t"):
10     day_string = row['timestamp'][0:10]
11
12     if day_string in edits_by_day:
13         edits_by_day[day_string] = edits_by_day[day_string] + 1
14     else:
15         edits_by_day[day_string] = 1
16
17 input_file.close()
18
19 # output the counts by day
20 output_file = open("hp_edits_by_day.tsv", "w", encoding='utf-8')
21
22 # write a header
23 output_file.write("date\tedits\n")
24
25 # iterate through every day and print out data into the file
26 for day_string in edits_by_day.keys():
27     output_file.write("\t".join([day_string, str(edits_by_day[day_string])]) + "\n")
28
29 output_file.close()

Benjamin Mako Hill || Want to submit a patch?