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

E

erikcw

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
 
F

FlipFish2007

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))
 
E

erikcw

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!

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!
 
C

Carsten Haese

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!

Since you're already using dictionaries, as an alternative solution you
could construct a single dictionary that is keyed on two-dimensional
tuples (also known as ordered pairs). Combine that with the setdefault
method to initialize new entries and you end up with something like
this:

def endElement(self, name):
if name == 'row':
entry = self.data.setdefault((self.parent,self.child),[])
entry.append(...)

-Carsten
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top