-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_csv.py
45 lines (36 loc) · 1.47 KB
/
parse_csv.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
"""From https://github.com/GSD6338/XmasTree/blob/main/03_execution/run.py"""
from csv import reader
def chunks(lst, n):
for i in range(0, len(lst), n):
yield lst[i:i+n]
def parse_csv(filename):
"""Returns a list of animation frames, which are lists containing 500 grb tuples corresponding
to each LEDs index.
"""
result = []
with open(filename, 'r') as read_obj:
# pass the file object to reader() to get the reader object
csv_reader = reader(read_obj)
# Iterate over each row in the csv using reader object
lineNumber = 0
for row in csv_reader:
# row variable is a list that represents a row in csv
# break up the list of rgb values
# remove the first item
if lineNumber > 0:
parsed_row = []
row.pop(0)
chunked_list = list(chunks(row, 3))
for element_num in range(len(chunked_list)):
# this is a single light
r = float(chunked_list[element_num][0])
g = float(chunked_list[element_num][1])
b = float(chunked_list[element_num][2])
light_val = (g, r, b)
# turn that led on
parsed_row.append(light_val)
# append that line to lightArray
result.append(parsed_row)
# time.sleep(0.03)
lineNumber += 1
return result