0

I have nested dictionary as 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]

..........

level 1 would be my key level 2 would be my key1 level 3 would be my key2 core would be my value2 for key2 core and level 3 would be my value1 for key1 core, level 3, and level 2 would be my value for key

When the script was executed, there were some errors, specifically KeyError of key2. I came up with couple lines to put this exception to a log.txt file. ie:

except KeyError:
  save_to_log (time_stamp, sys.exc_info()[0], sys.exc_info()[1])
  continue

However, it can only output the key2 information. I would like to be able to output the key information in respect to the key2 so that I can easily identify the fault in the original file. Is this possible in Python? Any suggestion is greatly appreciated. Thanks in advance.

5
  • oops. I noticed the format has changed a little and this may confuse people who tries to read the question. Here's a example of what I'm trying to say: I have a bookstore which has 100 books in it. Each book has between 5 to 30 chapters, which then has between 1 to 10 paragraphs. Now my program encounters an error saying that one of the paragraph is missing, but this paragraph can be in any chapter or any book. Once the paragraph is identified, is there a way Python can output the chapter and book info to a text file instead of just the paragraph info? Thanks Commented Sep 6, 2011 at 17:29
  • 1
    My favorite trick to avoid nested dicts/lists is to index a dict with a tuple. So you can say bookstore = {}; bookstore[book,chapter,paragraph] = "blah". This vastly simplifies this sort of thing. Commented Sep 6, 2011 at 17:41
  • If you do it this way, how do you "link" the book to the chapter then to the paragraph? Commented Sep 7, 2011 at 18:22
  • I don't know what you mean by link here. Under my scheme, you have to have the book, chapter, and paragraph identifiers "in hand" in order to look up the paragraph. Can you explain your use case and your error case a bit more? You're getting a KeyError on key2; where is your key2 coming from? Commented Sep 7, 2011 at 18:47
  • For "key" (bookstore) I have many "value" (books). Then I make each book a "key1" with many "value1" (chapters). Then I make each chapter a "key2" with many "value2" (paragraphs). If a chapter is missing ("key2" or "value1"), script gives me a exception.KeyError for "key2" and log it to a file. But I have many books with the same chapter number. I want to know if there is a way I can id the "key1" based on the "key2". Thanks Commented Sep 8, 2011 at 14:28

1 Answer 1

0

This should be easy to debug by stepping through with pdb

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

1 Comment

the bug is not in the script but in the text file script modifies. that's why I'm wondering if Python will be able to identify "which book" is missing the paragraph so we can go replace "the book". Thanks for the feedback though.

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.