-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount2.py
31 lines (24 loc) · 951 Bytes
/
count2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import csv
from collections import defaultdict
def ice_cream_counts(path):
"""Returns gender/preference counts from csv in path."""
counts = {'m': defaultdict(int), 'f': defaultdict(int)}
with open(path) as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
counts[row['gender']][row['preference']] += 1
return counts
def write_html(counts):
flavors = {'p': 'pistachio', 's': 'strawberry'}
heading = """
<!DOCTYPE html>
<html><head><title>Ice Cream Preferences</title></head><body>
<table border="1">
<tr> <th>preference</th> <th>female</th> <th>male</th> </tr>""".lstrip()
table_row = '<tr> <td>{}</td> <td>{}</td> <td>{}</td> </tr>'
print(heading)
for f, flavor in flavors.items():
print(table_row.format(flavor, counts['f'][f], counts['m'][f]))
print('</table></body></html>')
counts = ice_cream_counts('ice-cream.csv')
write_html(counts)