1

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?

EDIT:I solved it and wanted to share with people. This code gets the values and colours for X and Y cordinates of the shapes and stores them in the list. Thanks for the suggestions below:

import xml.etree.ElementTree as ET
import re 
r = re.compile('[0-9]{1,}')
root = ET.parse('pieces_A.xml').getroot()

line=[]
y=[]
X=[]
Y=[]
newlist=[]
c=[]
i=0


#gets the numbers and colours.
for child in root:
    line.append((child.attrib['d']))
    c.append(child.attrib)
    y.append((r.findall(line[i])))
    i +=1
#appends the colours and x,y cordinates to a new list
for i in range(len(y)):
    for  j in range(len(y[i])):
        if j%2==0:
            X.append(y[i][j])
        if j%2==1:
            Y.append(y[i][j])


    newlist.append([ X,Y,c[i]['fill'] ] )
    X=[]
    Y=[]

print(newlist)

So right now what is does is this for every item first 3 points are x cordinates and second points are for the y cordinates and the last element is the colour of the shape:

[[['50', '50', '90'], ['50', '90', '90'], 'red'], 
Can Uçar
  • 73
  • 5
  • 1
    I'd think [parsing the xml](http://stackoverflow.com/questions/1912434/how-do-i-parse-xml-in-python) and then spitting up just the field string would be easier then using re on the whole file. But I could be wrong. – Tadhg McDonald-Jensen May 03 '16 at 12:52
  • Thanks for your suggestion. At least I narrowed it down a bit. – Can Uçar May 03 '16 at 13:16

2 Answers2

0

I would use the ElementTree module in order to parse the file. It's much more simple to get the d= attributes this way and then you can parse them using a regex.

import xml.etree.ElementTree as ET

root = ET.parse('/path/to/file').getroot()

for child in root:
    print(child.attrib['d']) # store this as variable and then parse to your variables.
gr1zzly be4r
  • 2,072
  • 1
  • 18
  • 33
0

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.

Abbas
  • 3,872
  • 6
  • 36
  • 63