-1

I have an xml file with point coordinate like this

<vertex x="15" y="15.2"/>

I would like to scale these coordinate like

<vertex x="15*factor" y="15.2*factor"/>

with factor=0.2 (a number)

I was thinking about using re. Something like:

 re.sub(r'(x=\"[0-9]\"', X, X*factor, data)

I prefer not two loop over all lines, and split and so one...

since I'm reading the file as one string

f = open(filename)
data = f.read()
f.close()

EDIT:

More context

<polygon>
    <vertex x="-15" y="15"/>
    <vertex x="-15" y="-15"/>
</polygon>
<polygon>
    <vertex x="15" y="-15"/>
    <vertex x="15" y="15"/>
</polygon>
<polygon>
    <vertex x="-15" y="-15"/>
    <vertex x="15" y="-15"/>
</polygon>

Output for factor=0.1

<polygon>
    <vertex x="-1.5" y="1.5"/>
    <vertex x="-1.5" y="-1.5"/>
</polygon>
<polygon>
    <vertex x="1.5" y="-1.5"/>
    <vertex x="1.5" y="1.5"/>
</polygon>
<polygon>
    <vertex x="-1.5" y="-1.5"/>
    <vertex x="1.5" y="-1.5"/>
</polygon>

Any ideas?

Tengis
  • 2,721
  • 10
  • 36
  • 58

2 Answers2

0

I'm not so sure what might be desired output here, my guess is maybe we would start with an expression similar to:

(x=|y=)\"\s*([-]?[0-9]+)\s*\"

Test

import re

regex = r"(x=|y=)\"\s*([-]?[0-9]+)\s*\""

test_str = ("<polygon>\n"
    "    <vertex x=\"-15\" y=\"15\"/>\n"
    "    <vertex x=\"-15\" y=\"-15\"/>\n"
    "</polygon>\n"
    "<polygon>\n"
    "    <vertex x=\"15\" y=\"-15\"/>\n"
    "    <vertex x=\"15\" y=\"15\"/>\n"
    "</polygon>\n"
    "<polygon>\n"
    "    <vertex x=\"-15\" y=\"-15\"/>\n"
    "    <vertex x=\"15\" y=\"-15\"/>\n"
    "</polygon>")

subst = "\\1\"\\2\\*factor\""

result = re.sub(regex, subst, test_str, 0, re.MULTILINE)

if result:
    print (result)

In this demo, the expression is explained, if you might be interested.

Emma
  • 27,428
  • 11
  • 44
  • 69
  • 1
    That's nearly what I'm looking for. However, it seems that the *calculation* (number times factor) can not be performed, right? See my edit. – Tengis Jun 27 '19 at 18:03
  • 1
    You might reconsider parsing [x|html with regex](https://stackoverflow.com/a/1732454/1422451). – Parfait Jun 27 '19 at 20:31
  • 1
    @Emma although your answer is not exactly not what I wanted, but I thought it has great value. I learned some nice regex-stuff there. Thank you! – Tengis Jun 28 '19 at 05:38
0

Here

import xml.etree.ElementTree as ET


xml = '''<r><polygon>
    <vertex x="-15" y="15"/>
    <vertex x="-15" y="-15"/>
</polygon>
<polygon>
    <vertex x="15" y="-15"/>
    <vertex x="15" y="15"/>
</polygon>
<polygon>
    <vertex x="-15" y="-15"/>
    <vertex x="15" y="-15"/>
</polygon></r>'''


def scale(factor):
    root = ET.fromstring(xml)
    vertex_lst = root.findall('.//vertex')
    for vertex in vertex_lst:
        for attr in ['x','y']:
            vertex.attrib[attr] = str(int(vertex.attrib[attr]) * factor)
    ET.dump(root)


scale(0.1)

output

<r><polygon>
    <vertex x="-1.5" y="1.5" />
    <vertex x="-1.5" y="-1.5" />
</polygon>
<polygon>
    <vertex x="1.5" y="-1.5" />
    <vertex x="1.5" y="1.5" />
</polygon>
<polygon>
    <vertex x="-1.5" y="-1.5" />
    <vertex x="1.5" y="-1.5" />
</polygon></r>
balderman
  • 22,927
  • 7
  • 34
  • 52