Can Uçar Can Uçar - 7 months ago 10
Python Question

Reading and Storing the lines in XML file

I have a XML file like this:

<svg version="1.1" xmlns="http://www.w3.org/2000/svg">
<path d="M 50 50 L 50 90 L 90 90 z" fill="red"/>
<path d="M 160 170 L 160 130 L 120 130 z" fill="green"/>
<path d="M 200 30 L 180 30 L 180 50 L 220 50 z" fill="blue"/>
<path d="M 40 100 L 40 140 L 60 140 L 60 120 z" fill="yellow"/>
<path d="M 210 70 L 230 90 L 270 90 L 270 50 L 230 50 z" fill="purple"/>
<path d="M 180 130 L 180 170 L 220 210 L 240 190 z" fill="olive"/>
<path d="M 100 200 L 120 180 L 80 140 L 80 180 z" fill="magenta"/>
</svg>


These are the cordinates of the shapes that I have to work with.What I'm trying to do is the get ALL the cordinates of these shapes and store them seperately.In order to make math calculations.Like getting x1=50 x2=50 x3=90 y1=50 y2=90 y3=90 for the first one(red)

How can I compile these lines and store cordinates?

I tried to write something like this but It can't get the cordinates between.

import re
array =[]

def extract_text(line, pattern):
text = pattern.search(line)
if text:
title = text.groups()[0]
array.append(title[0])


titlepattern = re.compile('path d=[^"]*>([^"])"/>')
tagsattheend = re.compile('^"/>')

with open('folder.xml', 'r') as file:
line = file.readline()
for filelines in file:
if tagsattheend.search(filelines):
extract_text(line, titleattheend)
else:
extract_text(line, titlepattern)
line = filelines
extract_text(line, titlepattern)


Note: I'm not deleting the code above since somebody might find it useful

EDIT::.I've tried to do parsing in XML thanks to the suggestion like this.:

from xml.dom import minidom

xmldoc = minidom.parse('pieces_A.xml')
xml = xmldoc.getElementsByTagName('path')
for s in xml:
print(s.attributes['d'].value)


I am getting the values between like:

M 50 50 L 50 90 L 90 90 z
M 160 170 L 160 130 L 120 130 z
M 200 30 L 180 30 L 180 50 L 220 50 z
M 40 100 L 40 140 L 60 140 L 60 120 z
M 210 70 L 230 90 L 270 90 L 270 50 L 230 50 z
M 180 130 L 180 170 L 220 210 L 240 190 z
M 100 200 L 120 180 L 80 140 L 80 180 z


But still how do I store these values is a question mark for me.

Answer

One way to get the values is to get them in a list and this is how you can do it using regex:

import re
#Search for numbers
r = re.compile('[0-9]{1,}')
s = 'M 100 200 L 120 180 L 80 140 L 80 180 z'
r.findall(s)
#Returns a list of strings having numbers
['100', '200', '120', '180', '80', '140', '80', '180']
#Map the results to int to get integers
map(int, r.findall(s))
#Returns a list of integers
[100, 200, 120, 180, 80, 140, 80, 180]

And then you can do this inside a loop to get all the list of values in another list and process it further.