-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrows.py
38 lines (33 loc) · 1.06 KB
/
rows.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
import struct
from datetime import date, timedelta
def get_row_data(file, cols, offset, stream):
# First data appears after 26 bytes in the row (first bytes are a hash)
offset = offset + 25
file.seek(offset)
data = {}
for col in cols:
byts = file.read(col['length'])
offset += col['length']
if col['type'] == 'string':
buf = ''
for b in byts:
if b not in ['\x00', '\x01']:
buf += b.decode('utf-8', 'ignore')
val = buf
if col['type'] == 'float':
val = struct.unpack('d', byts[1:])[0]
if col['type'] == 'int':
val = struct.unpack('i', byts[1:])[0]
if col['type'] == 'date':
num = struct.unpack('i', byts[1:])[0]
if not num == 0:
d0 = date(1, 1, 1)
days = timedelta(days=num-1)
delta = d0 + days
val = delta
else:
val = None
data[col['name']] = val
if stream:
print(data)
return data