-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.py
More file actions
56 lines (44 loc) · 1.35 KB
/
Copy pathparse.py
File metadata and controls
56 lines (44 loc) · 1.35 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
50
51
52
53
54
55
56
def parse_file(datafile):
data = []
with open(datafile, "r") as f:
header = f.readline().split(",")
for line in f:
fields = line.split(",")
entry = {}
for i, value in enumerate(fields):
entry[header[i].strip()] = value.strip()
data.append(entry)
return data
import csv
def parse_csv(datafile):
data = []
with open(datafile, "r") as f:
r = csv.DictReader(f)
for line in r:
data.append(line)
return data
import xlrd
def parse_xls(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(1, 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.xldata_as_tuple(mintime, 0)
data = {
'maxtime': realtime,
'maxvalue': maxval,
'mintime':realmintime,
'minvalue': minval,
'avgcoast': sum(cv) / float(len(cv))
}
return data
data = parse_xls(datafile)
import pprint
pprint.pprint(data)