-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsvHandler.py
69 lines (51 loc) · 1.64 KB
/
csvHandler.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import csv
def readCsvFile(fileName: str) -> dict: # Read a csv file in the same folder as the python script and return to user
"""
Reads a file and divides headers and rows into a dictionary
\n
Can return a dictionary or None if file did not manage to open.
\n
Make sure to handle None exceptions
"""
print("begin reading csv...")
try:
file = open(fileName)
except OSError:
print("ERROR: Unable to open the file " + fileName)
return None
csvReader = csv.reader(file, delimiter=";")
header = []
rows = []
header = next(csvReader)
for row in csvReader:
rows.append(row)
csvDict = {
"header" : header,
"rows" : rows
}
print("Done!")
file.close()
return csvDict
def createCsvFile(data: dict, wantedFileName: str) -> str: # returns true if success and return false if error
"""
Creates a csv file from a dictionary. Can return 3 different messages.
* SUCCESS - If the function excecuted as intended.
* OPEN_FILE_ERROR - The function did not manage to open the file.
* WRITE_FILE_ERROR - The function did not manage to write to file.
\n
If a call to this function is made try to handle the different errors.
"""
print("begin writing to file...")
try:
file = open(wantedFileName, 'w+')
except:
return "OPEN_FILE_ERROR"
csvWriter = csv.writer(file)
try:
csvWriter.writerow(data["header"])
csvWriter.writerows(data["rows"])
except:
"WRITE_FILE_ERROR"
file.close()
print("done!")
return "SUCCESS"