Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Python Programming Language

Splitting SAX results


Hi list,

I have a very simple SAX script from which I get results like
'Title1:Description','Title2:Description'. I want to split each result
on the colon, using the two resulting elements as key/value pairs in a
dictionary. I've tried a couple different approaches with lists etc,
but I keep getting an 'IndexError: list index out of range' when I go
to split the results. Probably an easy fix but it's my first hack at
SAX/XML. Thank you!

from xml.sax import make_parser
from xml.sax.handler import ContentHandler

class reportHandler(ContentHandler):
  def __init__(self):
    self.isReport = 0

  def startElement(self, name, attrs):
    if name == 'title':
      self.isReport = 1
      self.reportText = ''

  def characters(self, ch):
    if self.isReport:
      self.reportText += ch

  def endElement(self, name):
    if name == 'title':
      self.isReport = 0
      print self.reportText

parser = make_parser()
parser.setContentHandler(reportHandler())
parser.parse('http://www.some.com/rss/')

IamIan wrote:
> I have a very simple SAX script from which I get results like
> 'Title1:Description','Title2:Description'. I want to split each result
> on the colon, using the two resulting elements as key/value pairs in a
> dictionary. I've tried a couple different approaches with lists etc,
> but I keep getting an 'IndexError: list index out of range' when I go
> to split the results. Probably an easy fix but it's my first hack at
> SAX/XML. Thank you!

Sounds like a problem with the data to me rather than SAX.

However, SAX tends to make things much more complex than necessary, so you
loose the sight on the real problems. Try a library like ElementTree or lxml
to make your life easier. You might especially like lxml.objectify.

http://effbot.org/zone/element.htm
http://effbot.org/zone/element-iterparse.htm

http://codespeak.net/lxml/dev/
http://codespeak.net/lxml/dev/objectify.html

Stefan

Well SAX isn't the problem... maybe I should repost this with a
different title. The SAX part works just as I want, but the results I
get back need to be manipulated. No matter what I try I can't split a
result like 'Title 1:Description' on the colon without getting an
IndexError. Ideas anyone?
On 6/8/07, IamIan <ian@gmail.com> wrote:

> Well SAX isn't the problem... maybe I should repost this with a
> different title. The SAX part works just as I want, but the results I
> get back need to be manipulated. No matter what I try I can't split a
> result like 'Title 1:Description' on the colon without getting an
> IndexError. Ideas anyone?

I don't think you've showed us any examples of the code you're having
trouble with.  I don't see anything in your original post that tries
to split strings. If you just want to know how split works, here's an
example:

>>> t = 'Title1:Description'
>>> key, value = t.split(':')
>>> print key
Title1
>>> print value
Description

If that doesn't help, show us a sample of some of the data you're
working with, what you've
tried so far, and what the end result is supposed to look like.

--
Jerry

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc