-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusing_csv_module.py
More file actions
49 lines (39 loc) · 1.41 KB
/
using_csv_module.py
File metadata and controls
49 lines (39 loc) · 1.41 KB
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
#!/usr/bin/env python
"""
Your task is to process the supplied file and use the csv module to extract data from it.
The data comes from NREL (National Renewable Energy Laboratory) website. Each file
contains information from one meteorological station, in particular - about amount of
solar and wind energy for each hour of day.
Note that the first line of the datafile is neither data entry, nor header. It is a line
describing the data source. You should extract the name of the station from it.
The data should be returned as a list of lists (not dictionaries).
You can use the csv modules "reader" method to get data in such format.
Another useful method is next() - to get the next line from the iterator.
You should only change the parse_file function.
"""
import csv
import os
DATADIR = ""
DATAFILE = "745090.csv"
def parse_file(datafile):
name = ""
data = []
with open(datafile,'rb') as f:
name = f.next()
f.next()
reader = csv.reader(f)
for i in reader:
data.append(i)
# Do not change the line below
name = name.split('"')
name = name[1]
return (name, data)
def test():
datafile = os.path.join(DATADIR, DATAFILE)
name, data = parse_file(datafile)
# assert name == "MOUNTAIN VIEW MOFFETT FLD NAS"
# assert data[0][1] == "01:00"
# assert data[2][0] == "01/01/2005"
# assert data[2][5] == "2"
if __name__ == "__main__":
test()