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

Parsing SVG in python

Expand Messages
  • Pranav Lal
    Hi all, Is it possible to parse SVG in python? I was trying to use the following code and not getting any output. try: import xml.etree.cElementTree as ET
    Message 1 of 3 , Jan 13, 2014
    • 0 Attachment
      Hi all,

      Is it possible to parse SVG in python? I was trying to use the following
      code and not getting any output.


      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

      The plan.svg file is at:
      https://dl.dropboxusercontent.com/u/3688386/plan.svg

      I have tried the above code with regular xml and the code works.
      Pranav
    • Bjoern Hoehrmann
      ... 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
      Message 2 of 3 , Jan 13, 2014
      • 0 Attachment
        * 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/
      • Pranav Lal
        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
        Message 3 of 3 , 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/
        Your message has been successfully submitted and would be delivered to recipients shortly.