-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilereading.py
27 lines (21 loc) · 897 Bytes
/
filereading.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
from typing import Generator
from data_record import DataRecord
DataRecordGenerator = Generator[DataRecord, None, None]
def read_by_records(source: str) -> DataRecordGenerator:
header_processed = False
with open(source) as file:
for line in file:
# split the comma seperated line
record = line.split(sep=',')
if not header_processed:
# if it is the first line then
# process it as a header
header = [x.rstrip() for x in record]
yield DataRecord(True, tuple(header))
header_processed = True
else:
# if it is not the first line
# then process it as floating
# point data
data = [float(x) for x in record]
yield DataRecord(False, tuple(data))