initial versions of solutions to harry potter challenges
[harrypotter-wikipedia-cdsw-solutions] / solution2-alternative.py
1 # Q: Who are the 5 most active editors on articles in Harry Potter? How may edits have they made?
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_editor = {}
9 for row in DictReader(input_file, delimiter="\t"):
10     user = row['user']
11
12     if user in edits_by_editor:
13         edits_by_editor[user] = edits_by_editor[user] + 1
14     else:
15         edits_by_editor[user] = 1
16
17 input_file.close()
18
19 # output the counts by day
20 output_file = open("hp_edits_by_user.tsv", "w", encoding='utf-8')
21
22 # write a header
23 output_file.write("user\tedits\n")
24
25 # iterate through every day and print out data into the file
26 for user in edits_by_editor:
27     output_file.write("\t".join([user, str(edits_by_editor[user])]) + "\n")
28
29 output_file.close()
30

Benjamin Mako Hill || Want to submit a patch?