Loading ...
Sorry, an error occurred while loading the content.

66298RE: [svg-developers] Parsing SVG in python

Expand Messages
  • Pranav Lal
    Jan 13, 2014
    • 0 Attachment
      Hi Björn,

      Many thanks. Your reference to lxml did the trick. Here is my code. It is a
      work in progress. I am storing all coordinates of lines and rectangles in a
      python list.
      import numpy as np
      from lxml import etree
      filename='plan.svg'
      tree = etree.parse(open(filename, 'r'))
      #mega list of all coordinates
      allPoints=[]
      for element in tree.iter():
      cleanTag=element.tag.split("}")[1]
      #handle a rectangle and a line
      if cleanTag=='line' or cleanTag=='rect':
      x1=element.get('x1')
      x2=element.get('x2')
      y1=element.get('y1')
      y2=element.get('y2')
      allPoints.append(x1)
      allPoints.append(y1)
      allPoints.append(x2)
      allPoints.append(y2)
      for item in allPoints:
      print item

      Pranav
      -----Original Message-----
      From: svg-developers@yahoogroups.com [mailto:svg-developers@yahoogroups.com]
      On Behalf Of Bjoern Hoehrmann
      Sent: Tuesday, January 14, 2014 12:36 AM
      To: svg-developers@yahoogroups.com
      Subject: Re: [svg-developers] Parsing SVG in python



      * Pranav Lal wrote:
      >try:
      > import xml.etree.cElementTree as ET
      >except ImportError:
      > import xml.etree.ElementTree as ET
      >
      >tree = ET.ElementTree(file='plan.svg')
      >root=tree.getroot()
      >
      >for elem in tree.iter(tag='rect'):
      > print elem.tag, elem.attrib

      You probably need to read up on namespace usage in ElementTree, e.g.
      http://stackoverflow.com/questions/14853243/ has some information.
      --
      Björn Höhrmann · mailto:bjoern@... · http://bjoern.hoehrmann.de
      Am Badedeich 7 · Telefon: +49(0)160/4415681 · http://www.bjoernsworld.de
      25899 Dagebüll · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
    • Show all 3 messages in this topic