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

erikcw erikwickstrom at gmail.com
Fri Mar 30 17:34:02 EDT 2007


On Mar 30, 5:23 pm, FlipFish2... at yahoo.com wrote:
> On Mar 30, 4:56 pm, "erikcw" <erikwickst... at gmail.com> 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))

That seems to have done the trick!  I can't believe I spent all
afternoon trying to figure that out!!  Thanks a million!




More information about the Python-list mailing list