Why does this cause KeyError: 0?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • josh001
    New Member
    • Jul 2008
    • 19

    Why does this cause KeyError: 0?

    I'm trying to figure out why KeyError: 0 pops up when I try to run the program containing the code.



    Code:
     
    OpenDLG = wx.FileDialog(
                self.win,
                message='Please choose the quiz or quizes that you wish to use.',
                defaultDir=os.getcwd(),
                defaultFile='',
                wildcard='*.*',
                style=wx.OPEN | wx.MULTIPLE | wx.CHANGE_DIR)
            self.AllWords = {}
            paths = OpenDLG.GetPaths()
            wordlistnum = -1
            if OpenDLG.ShowModal() == wx.ID_OK:
                for path in paths:
                    textfile = open(path)
                    words = textfile.readlines()
                    french = []
                    english = []
                    Pass = 'no'
                    wordlistnum +=1
                    for word in words:
                        if Pass == 'yes':
                            english.append(word[0:-2])
                        if '#english#' in word:
                            Pass = 'yes'
                        if Pass == 'no':
                            if '#english#' not in word:
                                french.append(word[0:-2])
                    self.AllWords.update(
                        {wordlistnum: {'French': french, 'English': english}})


    The TXT File that I select contains the fallowing text.



    Code:
     
    bonjour
    aurevoir
    salut
    #english#
    hello
    good bye
    hey
    In the hope that someone can help,
    Josh
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Josh,

    Can you post the full error message, including tracebacks? KeyError implies a problem with a dictionary key.

    Comment

    • kaarthikeyapreyan
      New Member
      • Apr 2007
      • 106

      #3
      KeyError

      Code:
      >>> foo={'a':1,'b':2}
      >>> foo['c']
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
      KeyError: 'c'
      Key error is raised when a mapping (dictionary) key is not found in the set of existing keys. I think you are trying to access a non-existing dictionary key; post your complete code and error in case u don find the solution ;)
      just in case you don prefer to get the key error use the get() method associated with the dictionary.get( ) returns a none value in case the key is not found
      Code:
      >>> value=foo.get('a')
      >>> print value
      1
      >>> value=foo.get('c')
      >>> print value
      None
      >>>

      Comment

      • josh001
        New Member
        • Jul 2008
        • 19

        #4
        Why is it empty?

        But why is self.AllWords empty?
        Is it something to do with the way I accessed the text file?

        Here is the full Error code.
        Code:
         
        Traceback (most recent call last):
          File "C:\Documents and Settings\Joshua Leihe\My Documents\My Projects\NewProjects\Joshua\Python projects\French quiz\French Quiz_17.pyw.pyw", line 261, in French
            self.GameLoop(evt)
          File "C:\(path in which I stored the program)\French Quiz_17.pyw.pyw", line 190, in GameLoop
            self.French = self.AllWords[0]['French']
        KeyError: 0

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          Try adding some print statements to determine if your script is doing what you expect.

          Comment

          • kaarthikeyapreyan
            New Member
            • Apr 2007
            • 106

            #6
            Resolved KeyError :)

            Originally posted by josh001
            I'm trying to figure out why KeyError: 0 pops up when I try to run the program containing the code.

            Code:
             
            OpenDLG = wx.FileDialog(
                        self.win,
                        message='Please choose the quiz or quizes that you wish to use.',
                        defaultDir=os.getcwd(),
                        defaultFile='',
                        wildcard='*.*',
                        style=wx.OPEN | wx.MULTIPLE | wx.CHANGE_DIR)
                    self.AllWords = {}
                    paths = OpenDLG.GetPaths()
                    wordlistnum = -1
                    if OpenDLG.ShowModal() == wx.ID_OK:
                        for path in paths:
                            ...
            Josh
            Get the paths only after the event in the dialog completes. There were no files that were returned from the file dialog in its initial state, and hence self.AllWords was empty when u accessed it which caused the KeyError. try doing it this way

            Code:
             
            OpenDLG = wx.FileDialog(
                        self.win,
                        message='Please choose the quiz or quizes that you wish to use.',
                        defaultDir=os.getcwd(),
                        defaultFile='',
                        wildcard='*.*',
                        style=wx.OPEN | wx.MULTIPLE | wx.CHANGE_DIR)
                    self.AllWords = {}
                    wordlistnum = -1
                    if OpenDLG.ShowModal() == wx.ID_OK:
                        paths = OpenDLG.GetPaths()
                        for path in paths:
                            ...

            Comment

            • josh001
              New Member
              • Jul 2008
              • 19

              #7
              Solved!

              That did it!
              Funny how things like that matter so much.
              I'll have to remember that.

              Thanks Josh

              Comment

              Working...