-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReadingExcelFiles.py
61 lines (44 loc) · 1.51 KB
/
ReadingExcelFiles.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
#!/usr/bin/python
"""
Your task is as follows:
- read the provided Excel file
- find and return the min, max and average values for the COAST region
- find and return the time value for the min and max entries
- the time values should be returned as Python tuples
Please see the test function for the expected return format
"""
import xlrd
from zipfile import ZipFile
import pprint
datafile = "data/2013_ERCOT_Hourly_Load_Data.xls"
def open_zip(datafile):
with ZipFile('{0}.zip'.format(datafile), 'r') as myzip:
myzip.extractall()
def parse_file(datafile):
workbook = xlrd.open_workbook(datafile)
sheet = workbook.sheet_by_index(0)
data = [[sheet.cell_value(r,col) for col in range(sheet.ncols)] for r in range(sheet.nrows)]
cv = sheet.col_values(1,start_rowx=1,end_rowx=None)
maxval = max(cv)
minval = min(cv)
maxpos = cv.index(maxval) + 1
minpos = cv.index(minval) + 1
maxtime = sheet.cell_value(maxpos,0)
realtime = xlrd.xldate_as_tuple(maxtime,0)
mintime = sheet.cell_value(minpos,0)
realmintime = xlrd.xldate_as_tuple(mintime,0)
data = {
'maxtime': realtime,
'maxvalue': maxval,
'mintime': realmintime,
'minvalue': minval,
'avgcoast': sum(cv) / float(len(cv))
}
return data
def test():
#open_zip(datafile)
data = parse_file(datafile)
pprint.pprint(data)
assert data['maxtime'] == (2013, 8, 13, 17, 0, 0)
assert round(data['maxvalue'], 10) == round(18779.02551, 10)
test()