-
Notifications
You must be signed in to change notification settings - Fork 5
/
file_handling.py
61 lines (58 loc) · 1.83 KB
/
file_handling.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
# In below statements, write 'a' for appending in file; 'w' for just writing in file; 'r' for just reading the file
file_name='coords.txt'
def write_in_file(digit, x, y):
lst=[digit, x, y]
with open(file_name,'a') as myfile:
myfile.write(",".join(map(str, lst)))
myfile.write('\n')
def getcoords(digit):
digit_as_char=str(digit)
f=open(file_name, 'r')
list_of_lines=f.readlines()
for j in range (len(list_of_lines)):
if list_of_lines[j][0]==digit_as_char:
i=list_of_lines[j][2]
x_found=''
counter=2;
while(i!=","):
x_found+=i
counter+=1
i=list_of_lines[j][counter]
else:
y_found=''
counter+=1
i=list_of_lines[j][counter]
while(i!='\n'):
y_found+=i
counter+=1
i=list_of_lines[j][counter]
return [int(x_found), int(y_found)]
print ("Digit not found")
return [-1]
def getdigit(x, y):
x_as_str=str(x)
y_as_str=str(y)
f=open(file_name, 'r')
list_of_lines=f.readlines()
for j in range (len(list_of_lines)):
i=list_of_lines[j][2]
x_found=''
counter=2;
while(i!=","):
x_found+=i
counter+=1
i=list_of_lines[j][counter]
else:
if x_found==x_as_str:
y_found=''
counter+=1
i=list_of_lines[j][counter]
while(i!='\n'):
y_found+=i
counter+=1
i=list_of_lines[j][counter]
else:
if y_found==y_as_str:
return int(list_of_lines[j][0])
print ("No number at this coordinate")
return -1