-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_handler.py
45 lines (33 loc) · 1.21 KB
/
file_handler.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
import csv
class FileHandler:
def __init__(self):
pass
def setFileList(self,files):
self.files=files
def getFileList(self):
return self.files
def loadCSV (self, file_path): #só lê chave e valor
with open(file_path, mode='r') as infile:
next(infile) #skip header
reader = csv.reader(infile)
self.data = {rows[0]:(rows[1]) for rows in reader}
infile.close()
def loadCSVList (self, file_path):
with open(file_path, mode='r') as infile:
next(infile)
reader = csv.reader(infile)
self.data = list(list(rec) for rec in csv.reader(infile, delimiter=','))
infile.close()
def writeCSVList (self, file_path, data):
csv.register_dialect('myDialect', delimiter = ',', lineterminator = '\n')
with open(file_path, 'w', newline='') as outfile:
outfile.write('Activity,Material,Database,Key,Amount\n') #header
writer = csv.writer(outfile, dialect='myDialect')
writer.writerows(data)
outfile.close()
def appendCSVList (self, file_path, data):
csv.register_dialect('myDialect', delimiter = ',', lineterminator = '\n')
with open(file_path, 'a', newline='') as outfile:
writer = csv.writer(outfile, dialect='myDialect')
writer.writerows(data)
outfile.close()