By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,814 Members | 1,111 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,814 IT Pros & Developers. It's quick & easy.

Why does this cause KeyError: 0?

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



Expand|Select|Wrap|Line Numbers
  1.  
  2. OpenDLG = wx.FileDialog(
  3.             self.win,
  4.             message='Please choose the quiz or quizes that you wish to use.',
  5.             defaultDir=os.getcwd(),
  6.             defaultFile='',
  7.             wildcard='*.*',
  8.             style=wx.OPEN | wx.MULTIPLE | wx.CHANGE_DIR)
  9.         self.AllWords = {}
  10.         paths = OpenDLG.GetPaths()
  11.         wordlistnum = -1
  12.         if OpenDLG.ShowModal() == wx.ID_OK:
  13.             for path in paths:
  14.                 textfile = open(path)
  15.                 words = textfile.readlines()
  16.                 french = []
  17.                 english = []
  18.                 Pass = 'no'
  19.                 wordlistnum +=1
  20.                 for word in words:
  21.                     if Pass == 'yes':
  22.                         english.append(word[0:-2])
  23.                     if '#english#' in word:
  24.                         Pass = 'yes'
  25.                     if Pass == 'no':
  26.                         if '#english#' not in word:
  27.                             french.append(word[0:-2])
  28.                 self.AllWords.update(
  29.                     {wordlistnum: {'French': french, 'English': english}})
  30.  


The TXT File that I select contains the fallowing text.



Expand|Select|Wrap|Line Numbers
  1.  
  2. bonjour
  3. aurevoir
  4. salut
  5. #english#
  6. hello
  7. good bye
  8. hey
  9.  
In the hope that someone can help,
Josh
Jan 23 '09 #1
Share this Question
Share on Google+
6 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
Josh,

Can you post the full error message, including tracebacks? KeyError implies a problem with a dictionary key.
Jan 23 '09 #2

kaarthikeyapreyan
100+
P: 107
Expand|Select|Wrap|Line Numbers
  1. >>> foo={'a':1,'b':2}
  2. >>> foo['c']
  3. Traceback (most recent call last):
  4.   File "<stdin>", line 1, in <module>
  5. 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
Expand|Select|Wrap|Line Numbers
  1. >>> value=foo.get('a')
  2. >>> print value
  3. 1
  4. >>> value=foo.get('c')
  5. >>> print value
  6. None
  7. >>> 
Jan 23 '09 #3

P: 19
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.
Expand|Select|Wrap|Line Numbers
  1.  
  2. Traceback (most recent call last):
  3.   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
  4.     self.GameLoop(evt)
  5.   File "C:\(path in which I stored the program)\French Quiz_17.pyw.pyw", line 190, in GameLoop
  6.     self.French = self.AllWords[0]['French']
  7. KeyError: 0
  8.  
Jan 23 '09 #4

bvdet
Expert Mod 2.5K+
P: 2,851
Try adding some print statements to determine if your script is doing what you expect.
Jan 23 '09 #5

kaarthikeyapreyan
100+
P: 107
@josh001
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

Expand|Select|Wrap|Line Numbers
  1.  
  2. OpenDLG = wx.FileDialog(
  3.             self.win,
  4.             message='Please choose the quiz or quizes that you wish to use.',
  5.             defaultDir=os.getcwd(),
  6.             defaultFile='',
  7.             wildcard='*.*',
  8.             style=wx.OPEN | wx.MULTIPLE | wx.CHANGE_DIR)
  9.         self.AllWords = {}
  10.         wordlistnum = -1
  11.         if OpenDLG.ShowModal() == wx.ID_OK:
  12.             paths = OpenDLG.GetPaths()
  13.             for path in paths:
  14.                 ...
  15.  
Jan 28 '09 #6

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

Thanks Josh
Jan 28 '09 #7

Post your reply

Sign in to post your reply or Sign up for a free account.