Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 15th, 2004, 04:31 AM
NewPythoner NewPythoner is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Location: Bombay, India
Posts: 159 NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 43 m 45 sec
Reputation Power: 7
Send a message via Yahoo to NewPythoner
IndexError

Hi,
Can someone find the bug in this code!

Code:
>>> strLine='#'
>>> dict={}
>>> fin=open('c:\\python23\\scripts\\','r')
>>> while strLine!='':
... 		strLine=fin.readline()
... 		if ''!=strLine:
... 			z=strLine.split('||')
... 			strKey=z[0]
... 			strValue=z[1][:-1]
... 			dict[strKey]=strValue
... 		else:raise IndexError


The error that I get is
Code:
Traceback (most recent call last):
  File "<interactive input>", line 6, in ?
IndexError: list index out of range


Can anyone spot the error?????

Many thanks,
Subha

Reply With Quote
  #2  
Old November 15th, 2004, 05:34 AM
Grim Archon's Avatar
Grim Archon Grim Archon is offline
Mini me.
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783 Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)  Folding Points: 1488 Folding Title: Novice Folder
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 8
Send a message via MSN to Grim Archon
yes, on line 6 you assume split produces what you want and don't handle when z has len == 1. That's what your traceback reported

Code:
>>> a = "hello".split("x")
>>> a
['hello']
>>> a = "hellox".split("x")
>>> a
['hello', '']
>>> 


grimey
__________________
*** Experimental Python Markup CGI V2 ***

Last edited by Grim Archon : November 15th, 2004 at 05:43 AM.

Reply With Quote
  #3  
Old November 15th, 2004, 05:46 AM
sfb sfb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 447 sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 1 h 43 m 45 sec
Reputation Power: 10
I'll try

Code:
>>> dict={}

This rebinds the builtin function "dict"...

Code:
>>> fin=open('c:\\python23\\scripts\\','r')

This seems to be trying to open a directory as if it was a file - I'd expect you to get an IOError here.

Code:
>>> while strLine!='':

This could be written as "while strLine:" because an empty string is evaluates as "False" for these kinds of tests, but it's just for ease of reading, and not really a problem.

Code:
... 		if ''!=strLine:

The same here with "if not strLine:"

Code:
... 			z=strLine.split('||')
... 			strKey=z[0]
... 			strValue=z[1][:-1]

Somewhere in here, strLine probably doesn't split into two pieces, so z[1] will not exist. At the start of the program, you say "strLine = '#'", then doing a split('||') will result in a single-item list, for instance.

You might want error checking here, or you might want to check the file you are reading for blank lines, etc.

Code:
... 			strValue=z[1][:-1]

This... well, have you tried it? It returns an empty string all the time, which is probably not what you want.
It takes the list z, then takes the single item at index 1.
Then, from that, it removes the last item from the string.

Code:
... 			dict[strKey]=strValue
... 		else:raise IndexError

Here, you are saying:
"While the line is not blank:
Read a line from the file
if the line is blank raise an IndexError
"

Which seems like a strange error to raise...


It looks like you are reading a file like this:
Code:
key1||value1
key2||value2


So you could try something like:

Code:
mydict = {}
fin = file('c:\\python23\\myfile.txt', 'rb')

for line in fin:
    if line:
        key, value = line.split('||')
        mydict[key] = value




Or, if the file might have some lines in the wrong format that you want to ignore, this might be more useful:

Code:
mydict = {}
fin = file('c:\\python23\\myfile.txt', 'rb')

for line in fin:
    if line:
        try:
            key, value = line.split('||')
            mydict[key] = value

        except ValueError:
            print "This line is not in the right format:"
            print line
            print



Reply With Quote
  #4  
Old November 15th, 2004, 05:49 AM
Grim Archon's Avatar
Grim Archon Grim Archon is offline
Mini me.
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783 Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)  Folding Points: 1488 Folding Title: Novice Folder
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 8
Send a message via MSN to Grim Archon
.... plus all the errors sfb mentioned

Reply With Quote
  #5  
Old November 15th, 2004, 05:57 AM
Grim Archon's Avatar
Grim Archon Grim Archon is offline
Mini me.
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783 Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)  Folding Points: 1488 Folding Title: Novice Folder
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 8
Send a message via MSN to Grim Archon
Code:
fin=open('c:\\python23\\scripts\\','r')

Can't see how you managed to enter this without getting an exception!

Reply With Quote
  #6  
Old November 15th, 2004, 06:15 AM
NewPythoner NewPythoner is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Location: Bombay, India
Posts: 159 NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 43 m 45 sec
Reputation Power: 7
Send a message via Yahoo to NewPythoner
Nooooooo that wasn't an error of the filename....a typo maybe.... I forgot to add the filename ...while typing in the query...I didn't paste the that particular file opening line .... and so I didn't receive any error there....thanks both of u....let me see if things work fine....


Thanks,
Subha

Reply With Quote
  #7  
Old November 15th, 2004, 06:24 AM
Grim Archon's Avatar
Grim Archon Grim Archon is offline
Mini me.
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783 Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)  Folding Points: 1488 Folding Title: Novice Folder
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 8
Send a message via MSN to Grim Archon
As you now know it is more helpful to you and us if you post real code, there is no point making up stuff by pasting it together.

grimey

Reply With Quote
  #8  
Old November 15th, 2004, 06:28 AM
NewPythoner NewPythoner is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Location: Bombay, India
Posts: 159 NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 43 m 45 sec
Reputation Power: 7
Send a message via Yahoo to NewPythoner
Always post the real code....this time I guess was being too smart...anyway....I'll keep ur sarcastic statement in mind!!!!

Thanks,
Subha

Reply With Quote
  #9  
Old November 15th, 2004, 06:32 AM
Grim Archon's Avatar
Grim Archon Grim Archon is offline
Mini me.
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783 Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)  Folding Points: 1488 Folding Title: Novice Folder
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 8
Send a message via MSN to Grim Archon

Reply With Quote
  #10  
Old November 15th, 2004, 07:00 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 11 m 13 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Yet another version ,

Code:
#!/usr/bin/env python

matrix = {}

for line in file('C:\Python23\myfile.txt'):
    if '||' in line:
        key, value = line.split('||')
        matrix[key] = value


This time, I do a simple check to see if the '||' characters are in line - so the split should go ahead without a glitch. If we can't split (because the '||' characters can't be found) then it just ignores the line.

Simple enough,

Mark.
__________________
programming language development: www.netytan.com Hula


Reply With Quote
  #11  
Old November 15th, 2004, 07:13 AM
NewPythoner NewPythoner is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Location: Bombay, India
Posts: 159 NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 43 m 45 sec
Reputation Power: 7
Send a message via Yahoo to NewPythoner
Thanks sfb....my problem is solved...thanks for the beautiful explanation!

Subha

Reply With Quote
  #12  
Old November 15th, 2004, 11:44 PM
NewPythoner NewPythoner is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Location: Bombay, India
Posts: 159 NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level)NewPythoner User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 43 m 45 sec
Reputation Power: 7
Send a message via Yahoo to NewPythoner
Thanks Mark .... you've reduced the lines of code drastically...and yes readable too ....thats great!


Subha

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > IndexError


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump