forked from arrafi-musabbir/A-star-search-algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFILEREAD.py
39 lines (33 loc) · 1.17 KB
/
FILEREAD.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
'''
the following section reads from a text file that contains a graph of cities
and cost between cities
'''
class FileRead:
# this class takes a weighted txt file and initilizes it in a dictionary
def __init__(self):
self.wd = dict()
def readFile(self, path):
# this method read a weighted file and initializes it to a dictionary
with open(path, 'r') as f:
ltemp = 0
for i in f:
ltemp = i.split()
if len(ltemp) == 1:
self.wd[ltemp[0]] = None
else:
self.wd[ltemp[0]] = dict()
i = 1
while True:
if i + 2 >= len(ltemp):
break
self.wd[ltemp[0]][ltemp[i]] = list()
self.wd[ltemp[0]][ltemp[i]].append(float(ltemp[i + 1]))
self.wd[ltemp[0]][ltemp[i]].append(float(ltemp[i + 2]))
i = i + 3
return self.wd
def showdict(self):
print("hello")
print(self.wd)
for i in self.wd:
print(i, ':', self.wd[i])
return None