473,396 Members | 1,792 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Why does this cause KeyError: 0?

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
6 55531
bvdet
2,851 Expert Mod 2GB
Josh,

Can you post the full error message, including tracebacks? KeyError implies a problem with a dictionary key.
Jan 23 '09 #2
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
josh001
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
2,851 Expert Mod 2GB
Try adding some print statements to determine if your script is doing what you expect.
Jan 23 '09 #5
@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
josh001
19
That did it!
Funny how things like that matter so much.
I'll have to remember that.

Thanks Josh
Jan 28 '09 #7

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

Similar topics

0
by: Norman Shelley | last post by:
Is there a way to get at the object one is trying to access with a key to list out all the possible keys? See the except clause below. #!/usr/bin/env python2.2 from inspect import * import sys...
1
by: | last post by:
Hello. Maybe someone will help me with this KeyError: ............................. Traceback (most recent call last): File "C:\Python\tabla.py", line 929, in -toplevel- tablesDirectory =...
3
by: Rune Strand | last post by:
I'm experiencing strange errors both with pickle and cPickle in the below code: import cPickle as pickle #import pickle from string import ascii_uppercase from string import ascii_lowercase...
4
by: homepricemaps | last post by:
i have an href which looks like this: <td class="all"> <a class="btn" name="D1" href="http://www.cnn.com"> </a> here is my code
18
by: Seeker | last post by:
I essentially copied this program from K&R: #include <stdio.h> #include <stdlib.h> int main() { int c;
10
by: Mythmon | last post by:
I am trying to make a program that will basically simulate a chess clock in python. To do this I have two threads running, one that updates the currently running clock, and one that watches for a...
3
by: nyenyec | last post by:
urllib.quote chokes on unicode in 2.4.4. 2.4.4 (#1, Oct 18 2006, 10:34:39) Traceback (most recent call last): File "<stdin>", line 1, in ? File...
92
by: Heinrich Pumpernickel | last post by:
what does this warning mean ? #include <stdio.h> int main() { long l = 100; printf("l is %li\n", l * 10L);
2
by: christof | last post by:
Hi, I am using pickle/unpickle to let my program save its documents to disk. While this it worked stable for a long time, one of my users now complained, that he had a file which can't be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.