-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
101 lines (53 loc) · 2.79 KB
/
main.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
def main():
# lists holding values of x and y coordinates, in pixels
pixels_x = []
pixels_y = []
# l is a list of lines and i is used to tell whether current line is a line with wanted data (0 -> it's not, 1 -> it is)
l = []
i = 0
# f represents the .svg file, don't forget to specify your own path! :)
f = open("sample.svg", "r")
# for loop that goes over every line in code, deletes unwanted "C" at the beginning & '"' at the end, and stores line's values in lists
# (x to pixels_x, y to pixels_y)
for line in f.readlines():
# l represents one line, as the loop goes, every line becomes l for one cycle
l = line.split()
# if that skips line when it's empty
if len(l) == 0:
continue
# if that looks for unwanted "C". If found, value of i is changed to one -> if with i == 0 is no longer true ->
# -> whole loop is executed and data extraction begins
if l[0] == "C":
i = 1
l = l[1:]
# if unwanted "C" (the beginning of data) hasn't been found yet, the loop checks the next line without doing anything
if i == 0:
continue
# if that looks for "/>" (the end of data) and removes it together with '"' when found. The value of i is changed back to 0
# to skip whatever follows after the data
if l[-1] == "/>":
i = 0
l = l[:-1]
l[-1] = l[-1][:-1]
# the data is further split into x and y values, that are then appended to lists pixels_x and pixels_y
# j represents index in l list
for j in range(0, len(l)):
x, y = l[j].split(",")
pixels_x.append(x)
pixels_y.append(y)
# f (the .svg file) gets closed as we don't need it anymore
f.close()
# e represents .txt file where the extracted data will be written, don't forget to specify your own path!
e = open("sample.txt", "w")
# the text in the brackets get's displayed at the beginning of the .txt file, to help users understand its contents
e.write("# x, y in pixels")
# for loops that writes every pair of coordinates with the same index next to each other in the .txt file
# j is the number of coordinates that will be written to the .txt file
for j in range(0, len(pixels_x)):
e.write("%s %s\n" % (pixels_x[j], pixels_y[j]))
# as we no longer need e, it gets closed
e.close()
# this little trick makes an accessible library from code above and executes main()
# (code will work even when line n. 99 gets removed and line n. 100 unintended, dw)
if __name__ == '__main__':
main()