2

This is really a rookie question, but I couldn't figure out how to debug so here I am. The Python official document says: "exception KeyError: Raised when a mapping (dictionary) key is not found in the set of existing keys." Simple enough.

I'm writing Python code to generate a nested dictionary, starting with the outer dictionary and working inwards. Below is the code:

dict1 = {}

# retrieve information from the database
rs = db.execute("select id_stn, id_typ, id_dev, id from point where id='keyword1' and id_typ<>'keyword2' and id_typ<>'keyword3' and good=1")

# adding information to dictionay
for line in rs:
    arg = [x.strip() for x in line]

    if arg[0] not in stnList:
        continue

    typ = arg[1]

    if arg[0] not in dict1:
        dict1[arg[0]] = {}

    if Typ not in dict1[arg[0]]:
        dict1[arg[0]][typ] = {}

    dev = arg[2]

    if typ == 'TYPE1':
        dev = arg[2].replace('_M','').replace('_N','')
        m = pattern.search(dev)
        if m:
            dev = m.group(1)

    dict1[arg[0]][typ][dev] = []

    newTyp = 'CK' + typ

    if newTyp not in dict1[arg[0]]:
        dict1[arg[0]][newTyp] = {}

    dict1[arg[0]][newTyp][dev] = arg[:]

The code compiles and executes fine. But, when I added the following to the code:

for line in avr:
    print (dict1[arg[0]], dict1[newTyp], dict1[dev], arg)

I got the KeyError saying one of the "newTyp" is not in the list. I triple checked and the key is for sure there in the "newTyp" list. Then, if I just do:

print (dict1)

It prints out everything fine, but not in a nice listed view that I would like to have. I'd like to have the output displayed like the following:

Dict1 | level 1-1  | level 2-1-1 | level 3-1-1-1 | core [1]
                                                 | core [2]
                                                 | core [3]
                                 | level 3-1-1-2 | core [1]
                                                 | core [2]
                   | level 2-1-2 | level 3-1-2-1 | core [1]
                                                 | core [2]
                                 | level 3-1-2-2 | core [1]
      | level 1-2  | level 2-2-1 | level 3-2-1-1 | core [1]
                                 | level 3-2-1-2 | core [1]
                   | level 2-2-2 | level 3-2-2-1 | core [1]
                                                 | core [2]
                                 | level 3-2-2-2 | core [1]
  ..........

Did I miss anything? How can I generate a nice formatted output?

Thanks in advance :)

1
  • Where does avr come from for the loop your print statement is in? Commented Aug 31, 2011 at 17:15

1 Answer 1

3

Try the following:

for line in avr:
    try:
        print (dict1[arg[0]], dict1[newTyp], dict1[dev], arg)
    except KeyError:
        import sys, pdb
        pdb.post_mortem(sys.exc_info()[2])

Then run it with the process still at a console and you can inspect the stack at the point of your error and see what's really going on.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the reply Ross. I've tried it, but it just gave me this: > line 123 <module>() -> print (dict1[arg[0]], dict1[newTyp], dict1[dev], arg) (Pdb) What should I do now? How I can check the stack?
Read the pdb docs. If you have trouble understanding them, then ask a new question with more specific s.
Hi Ross, sorry for the stupid questions. But how do I read the pdb file? Where is the file saved to? I'm using Python IDLE on Windows XP machine. Thanks
Did you read the pdb docs I linked to? If so, what do you mean by "pdb file"? Take your time for formulate a complete question, then ask it in a new question, not here in the comments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.