-
Notifications
You must be signed in to change notification settings - Fork 0
/
parseRelation.py
44 lines (40 loc) · 1.4 KB
/
parseRelation.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
# parse input realtions
def parseRelation(relations: str) -> dict:
# Splitting the data
parts = relations.split("}")
# initialize table
tables = {}
# get the table
for part in parts[:-1]:
part = part + "}"
# get the "Student"
equalIndex = part.find("=")
tableName = part[:equalIndex].strip()
# add into the tables
tables[tableName] = {}
# get the data between { }
lines = part.strip().split("\n")[1:-1]
lines = [line for line in lines if line != ""]
# create column of the table
headers = [header.strip() for header in lines[0].split(",")]
for i in range(len(headers)):
tables[tableName][headers[i]] = []
# get each column data
for line in lines[1:]:
values = line.strip().split(",")
values = [value.strip().strip("'") for value in values]
if values[i] == "NULL":
tables[tableName][headers[i]].append(None)
else:
if isStringFloat(values[i]):
tables[tableName][headers[i]].append(float(values[i]))
else:
tables[tableName][headers[i]].append(values[i])
return tables
# check if is float
def isStringFloat(s):
try:
float(s)
return True
except ValueError:
return False