forked from justmarkham/DAT4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
02_file_io_homework_solution.py
94 lines (74 loc) · 2.35 KB
/
02_file_io_homework_solution.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
'''
HOMEWORK SOLUTION: Reading and Writing Files in Python
'''
'''
PART 1:
Read in drinks.csv
Store the header in a list called 'header'
Store the data in a list of lists called 'data'
Hint: you've already seen this code!
'''
import csv
with open('../data/drinks.csv', 'rU') as f:
header = csv.reader(f).next()
data = [row for row in csv.reader(f)]
'''
PART 2:
Isolate the beer_servings column in a list of integers called 'beers'
Hint: you can use a list comprehension to do this in one line
Expected output:
beers == [0, 89, ..., 32, 64]
len(beers) == 193
'''
beers = [int(row[1]) for row in data]
'''
PART 3:
Create separate lists of NA and EU beer servings: 'NA_beers', 'EU_beers'
Hint: you can use a list comprehension with a condition
Expected output:
NA_beers == [102, 122, ..., 197, 249]
len(NA_beers) == 23
EU_beers == [89, 245, ..., 206, 219]
len(EU_beers) == 45
'''
NA_beers = [int(row[1]) for row in data if row[5]=='NA']
EU_beers = [int(row[1]) for row in data if row[5]=='EU']
'''
PART 4:
Calculate the average NA and EU beer servings to 2 decimals: 'NA_avg', 'EU_avg'
Hint: don't forget about data types!
Expected output:
NA_avg == 145.43
EU_avg == 193.78
'''
NA_avg = round(sum(NA_beers) / float(len(NA_beers)), 2)
EU_avg = round(sum(EU_beers) / float(len(EU_beers)), 2)
'''
PART 5:
Write a CSV file called 'avg_beer.csv' with two columns and three rows.
The first row is the column headers: 'continent', 'avg_beer'
The second and third rows contain the NA and EU values.
Hint: think about what data structure will make this easy
Expected output (in the actual file):
continent,avg_beer
NA,145.43
EU,193.78
'''
output = [['continent', 'avg_beer'], ['NA', NA_avg], ['EU', EU_avg]]
with open('avg_beer.csv', 'wb') as f:
csv.writer(f).writerows(output)
'''
BONUS:
Learn csv.DictReader() and use it to redo Parts 1, 2, and 3.
'''
# Part 1
with open('../data/drinks.csv', 'rU') as f:
data = [row for row in csv.DictReader(f)]
# Note: storing the header isn't actually useful for parts 2 and 3
# Also note: dictionaries are unordered, so don't rely on the ordering
header = data[0].keys()
# Part 2
beers = [int(row['beer_servings']) for row in data]
# Part 3
NA_beers = [int(row['beer_servings']) for row in data if row['continent']=='NA']
EU_beers = [int(row['beer_servings']) for row in data if row['continent']=='EU']