21

I have a simple for loop in Python that is exiting on exceptions even though the exception block contains a continue. There are still about 10 lines left to read when it hits an IndexError and exits the for loop. What am I missing here?

for row in hkx:  ##'hkx' are rows being read in from 'csv.open'
    try:
        print row[2],row[4]
    except IndexError, e:
        print 'Error:',e
        print 'Row Data:',len(row),row
        continue  ## I thought this would just move on to the next row in 'hkx' 

(sorry, total Python newbie here…) Thanks in advance!

5
  • 11
    It should works, are you sure there is not another exception (not only the IndexError) raised ? Commented Nov 28, 2011 at 8:23
  • 1
    @Nathan: Can you please post a complete, minimal example? Commented Nov 28, 2011 at 8:39
  • 1
    Agree with @CédricJulien - could you maybe post the output? Commented Nov 28, 2011 at 8:39
  • @Bjorn - there's not much else to it. These 3 lines precede the for loop: wrhk = csv.reader(open(WRHKcsv,'rbU')) hkx = csv.reader(open(HKXcsv,'rb')) wrhk24 = set((row[2],row[4]) for row in wrhk) & there's a code section that uses xlrd to convert an xls file to csv. Commented Nov 28, 2011 at 16:43
  • @Marcin - that's the only error it tells me about. I added the output in the comments below. Commented Nov 28, 2011 at 16:45

1 Answer 1

12

It does exactly as it should and continues with the next line. If an exception is terminating your code early then it must either not be IndexError, or it must be being thrown from some code outside the try: block.

>>> hkx = [ range(5), range(4), range(4), range(5) ]
>>> for row in hkx:  ##'hkx' are rows being read in from 'csv.open'
    try:
        print row[2],row[4]
    except IndexError, e:
        print 'Error:',e
        print 'Row Data:',len(row),row
        continue  ## I thought this would just move on to the next row in 'hkx'

2 4
2 Error: list index out of range
Row Data: 4 [0, 1, 2, 3]
2 Error: list index out of range
Row Data: 4 [0, 1, 2, 3]
2 4
>>> 

Note that if the row contains at least 3 items you'll get half of your printout, if you use a format string you can avoid that. (e.g. print "{} {}".format(row[2],row[4]))

You haven't said how hkx is defined except that it comes from csv.open. If it is a generator rather than a simple list then it might be that simply iterating over it throws IndexError. In that case you wouldn't catch that but the stack dump would show the line with the for row in hkx.

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

3 Comments

hkx is defined here: hkx = csv.reader(open(HKXcsv,'rb')) You can see from the last couple line of output the error. The line 'Row Data' shows the contents of the line causing the exception. 'Row Data' should contain about 8 elements, not just 'Zhenngzhou'. it seems to happen when ever there is a duplicate name in row[4]. Output: 8331 GU Qun 8331 LUI Wing Hong Edward 8331 BAO Zhi Chao 8099 BAO Hong Wei Error: list index out of range Row Data: 1 ['Zhengzhou']
What is in the csv file immediately after the Zhengzhou? Could there be a Ctrl-Z character in the file? The csv module will regard Ctrl-Z as terminating the file (even though you opened it in binary mode). In particular are there any non ascii characters that could have been encoded such that there's a byte with the value 26?
Well, I resolved this by skipping csv.open altogether & just reading the row data back from a list. Faster too.

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.