python - Reading in a xml file and regex matching to find dimensions: Nonetype error -
i've been working on project taking in .svg files , finding dimensions. instead of converting them png, or using pysvg see (best way of getting swf file dimensions python) not trying scan xml dimensions.
i've been using open("thefile").read() , cannot see why code not working. issue regex?
an illustration of xml file might this:
str: <?xml version="1.0" encoding="utf-8" standalone="no"?> <svg xmlns:xlink="http://www.w3.org/1999/xlink" height="25.6px" width="74.9px" xmlns="http://www.w3.org/2000/svg"> <g transform="matrix(1, 0, 0, 1, 0.5, 0.5)"> <path d="m71.3 0.15 q73.9 0.75 73.9 4.0 l73.9 20.6 q73.9 24.6 69.9 24.6 l4.0 24.6 q0.9 24.6 0.2 22.2 l0.0 20.6 0.0 4.0 0.2 2.4 q0.7 0.6 2.6 0.15 l4.0 0.0 69.9 0.0 71.3 0.15" fill="#ffffff" fill-rule="evenodd" stroke="none"/> <path d="m71.3 0.15 l69.9 0.0 4.0 0.0 2.6 0.15 q0.7 0.6 0.2 2.4 l0.0 4.0 0.0 20.6 0.2 22.2 q0.9 24.6 4.0 24.6 l69.9 24.6 q73.9 24.6 73.9 20.6 l73.9 4.0 q73.9 0.75 71.3 0.15" fill="none" stroke="#8e8e8e" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.0"/> </g> </svg>
the code error occurs:
x=re.findall("width=\"[^\"]*",svgf)[0]
my variable x found , equal 74.9px in case. i've been looking for. don't see error coming from. if allow error occur info want extracted. ideas?
the error:
error evaluating: thread_id: pid54226_seq2 frame_id: 140505356028928 scope: look attrs: svgf traceback (most recent phone call last): file "/applications/eclipse/plugins/org.python.pydev_3.4.1.201403181715/pysrc/pydevd_vars.py", line 422, in resolvecompoundvariable homecoming resolver.getdictionary(var) attributeerror: 'nonetype' object has no attribute 'getdictionary'
svg xml document, shall utilize standard tools xml. regexps not serving in these situations.
following illustration uses xpath , lxml
library (install first)
>>> xmlstr = """<?xml version="1.0" encoding="utf-8" standalone="no"?> ... <svg xmlns:xlink="http://www.w3.org/1999/xlink" height="25.6px" width="74.9px" xmlns="http://www.w3.org/2000/svg"> ... <g transform="matrix(1, 0, 0, 1, 0.5, 0.5)"> ... <path d="m71.3 0.15 q73.9 0.75 73.9 4.0 l73.9 20.6 q73.9 24.6 69.9 24.6 l4.0 24.6 q0.9 24.6 0.2 22.2 l0.0 20.6 0.0 4.0 0.2 2.4 q0.7 0.6 2.6 0.15 l4.0 0.0 69.9 0.0 71.3 0.15" fill="#ffffff" fill-rule="evenodd" stroke="none"/> ... <path d="m71.3 0.15 l69.9 0.0 4.0 0.0 2.6 0.15 q0.7 0.6 0.2 2.4 l0.0 4.0 0.0 20.6 0.2 22.2 q0.9 24.6 4.0 24.6 l69.9 24.6 q73.9 24.6 73.9 20.6 l73.9 4.0 q73.9 0.75 71.3 0.15" fill="none" stroke="#8e8e8e" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.0"/> ... </g> ... </svg>""" ... >>> lxml import etree >>> svg = etree.fromstring(xmlstr) >>> svg <element {http://www.w3.org/2000/svg}svg @ 0x7f7d180d2638> >>> svg.xpath("//@width") ['74.9px']
python regex svg
No comments:
Post a Comment