forked from justmarkham/DAT4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
02_file_io_homework.py
67 lines (51 loc) · 1.33 KB
/
02_file_io_homework.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
'''
HOMEWORK: 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!
'''
'''
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
'''
'''
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
'''
'''
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
'''
'''
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
'''
'''
BONUS:
Learn csv.DictReader() and use it to redo Parts 1, 2, and 3.
'''