-
Notifications
You must be signed in to change notification settings - Fork 1
/
csv_io.py
36 lines (32 loc) · 968 Bytes
/
csv_io.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
import csv
import sys
def csv_read(path):
try:
file = open(path, 'r')
csv_file = csv.reader(file)
id_list = []
label_list = []
next(csv_file)
index = -1
for stu in csv_file:
index += 1
id_list.append(stu[0])
label = stu[1].split(' ')
label_list.append([False for i in range(28)])
for i in label:
label_list[index][int(i)] = True
file.close()
return id_list, label_list
except IOError:
print("Error! Please check path for csv.")
sys.exit(0)
def csv_writer(path, img, label):
for i in range(len(label)):
label[i] = " ".join(str(x) for x in label[i])
csvfile = open(path, 'w', newline = "")
writer = csv.writer(csvfile)
writer.writerow(["Id","Predicted"])
length = len(img)
for i in range(length):
writer.writerow([img[i], label[i]])
csvfile.close()