python find image with path in file
<p>I'd like to find svgs or pngs withing a file. The images are in a attribue v="..."</p>

<p>A part of the file looks like this:</p>

<pre><code>&lt;symbol alpha="1" type="marker" name="0"&gt;
&lt;layer pass="0" class="SvgMarker" locked="0"&gt;
&lt;prop k="angle" v="0"/&gt;
&lt;prop k="fill" v="#000000"/&gt;
&lt;prop k="name" v="../Downloads/Inkscape_vectorisation_test.svg"/&gt;
&lt;prop k="offset" v="0,0"/&gt;
&lt;prop k="offset_unit" v="MM"/&gt;
&lt;prop k="outline" v="#000000"/&gt;
&lt;prop k="outline-width" v="1"/&gt;
&lt;prop k="outline_width_unit" v="MM"/&gt;
&lt;prop k="size" v="hello.png"/&gt;
&lt;prop k="size_unit" v="MM"/&gt;
&lt;/layer&gt;
</code></pre>

<p>I'd like to get a list like:</p>

<p>['../Downloads/Inkscape_vectorisation_test.svg','hello.png']</p>

<p>My python code:</p>

<pre><code>import re
projectFile = open("project.xml", "r")
regex = re.compile(r'(?&lt;=v\=").+(\.svg|\.png)(?="/&gt;)')

for line in projectFile:
    result = regex.findall(line)            
    for filename in result:
        print filename
</code></pre>

<p>I've tested my regex with <a href="http://pythex.org/" rel="nofollow">http://pythex.org/</a> which work fine but in python console the result is just <code>.svg</code> it seems the capturing group <code>(\.svg|\.png)</code> is interpreted differently. What am I doing wrong?</p>

<p>Is using regex a requirement? In case it isn't, an easier and cleaner approach would be to use <a href="http://lxml.de/" rel="nofollow">lxml</a>.</p>

<p>As it seems that the URIs you want appear in <code>prop</code> elements where <code>k="name"</code>, you could use <a href="http://lxml.de/xpathxslt.html" rel="nofollow">xpath</a> to do something like:</p>

<pre><code>from lxml import etree
f = etree.parse(projectFile)
root = f.getroot()
# This will give you a list with all prop elements that contain the URIs you want in the v attribute
elements = root.xpath("//prop[@k='name']")
</code></pre>

python regex