Re: I can't get multi-dimensional array to work...



On Mar 30, 4:56 pm, "erikcw" <erikwickst...@xxxxxxxxx> wrote:
Hi,

I'm trying to create a multidimensional data structure. However, id
doesn't seem to work past the 2nd dimension.

Here is my method:

def endElement(self, name):
if name == 'row' :
if not self.data.has_key(self.parent):
self.data[self.parent] = {}
elif not self.data[self.parent].has_key(self.child):
self.data[self.parent][self.child] = []
self.data[self.parent]
[self.child].append((self.creativeid, self.clicks, self.imps))

I've tried all kinds of different variations, and I keep getting the
same result:

Traceback (most recent call last):
File "sax.py", line 123, in ?
parser.parse(open('report.xml'))
File "/usr/lib/python2.4/site-packages/_xmlplus/sax/expatreader.py",
line 109, in parse
xmlreader.IncrementalParser.parse(self, source)
File "/usr/lib/python2.4/site-packages/_xmlplus/sax/xmlreader.py",
line 123, in parse
self.feed(buffer)
File "/usr/lib/python2.4/site-packages/_xmlplus/sax/expatreader.py",
line 216, in feed
self._parser.Parse(data, isFinal)
File "/usr/lib/python2.4/site-packages/_xmlplus/sax/expatreader.py",
line 315, in end_element
self._cont_handler.endElement(name)
File "sax.py", line 51, in endElement
self.data[self.parent][self.child].append((self.creativeid,
self.clicks, self.imps))
KeyError: u'Pickup Trucks'

I have a lot of php experience - am I accidentally doing a "php thing"
in my code?

Thanks so much for your help!

Erik

I haven't tested it, but superficially I'd suggest giving this a try:

def endElement(self, name):
if name == 'row' :
if not self.data.has_key(self.parent):
self.data[self.parent] = {}
if not self.data[self.parent].has_key(self.child):
self.data[self.parent][self.child] = []
self.data[self.parent]
[self.child].append((self.creativeid, self.clicks, self.imps))



.



Relevant Pages

  • I cant get multi-dimensional array to work...
    ... doesn't seem to work past the 2nd dimension. ... def endElement: ... line 109, in parse ... I have a lot of php experience - am I accidentally doing a "php thing" ...
    (comp.lang.python)
  • Re: Too many open files on OpenBSD
    ... #> def parse() ... Opens the file, optionally seeks to the given offset, then returns ...
    (comp.lang.ruby)
  • XML Newbie needing some serious help..
    ... nice XML output which looks similar to the bottom of this message.. ... Now I want to parse and get some information from it so here is what I ... def CPUType: ... interrupt controller ...
    (comp.lang.python)
  • Re: Regexp problem for template language
    ... Using split instead of scan and a slightly different regex: ... brackets like this please parse me"], ... def test_tokenization_without_curly ...
    (comp.lang.ruby)
  • Re: Indentation vs. "end"s
    ... Even if "end if" is no longer feasible to parse because of the if ... modifier there is still the possibility to have optional "end def" and ... comments after every 'end' of a deep embedded structure to make the ...
    (comp.lang.ruby)