简体   繁体   中英

Python exception KeyError question

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM