-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcsv_parser.py
48 lines (42 loc) · 1.48 KB
/
csv_parser.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
# Dawsin Blanchard, Sam Braga, Brian Couture, and Ethan Trott
# COS226 University of Maine
import csv, hashlib
# this variable allows us to limit the number of rows we parse
# (for convenience in debugging)
row_limit = -1
# returns the total number of rows in the file
def count_lines(filename):
with open(filename, "r") as csvfile:
return sum(1 for row in csvfile) - 1
# this will yield each Plate ID within our row_limit when iterated over
def get_plates(filename):
with open(filename, "r") as csvfile:
datareader = csv.reader(csvfile)
count = -1
for row in datareader:
if (count == -1):
#ignore the header row
count += 1
elif (row_limit == -1 or count < row_limit):
yield row[1]
count += 1
else:
return
#creates the SHA256 hashes of the plates and saves them to hashes.csv
def create_hashes(filename):
hashes = []
with open('hashes.csv', 'w') as file:
for plate in get_plates(filename):
#get the sha256 hash of the value
hash = hashlib.sha256(str(plate).encode('utf-8'))
#store the hash in the list
hashes.append(hash.hexdigest())
#write the hash to the file
file.write(hash.hexdigest()+"\n")
return hashes
def load_hashes():
f = open("hashes.csv", "r")
if (row_limit > 0):
return f.readlines(row_limit*65)
else:
return f.readlines()