tags:

views:

52

answers:

3

Sorry, I agree that was really poorly written:

Take 2: I have many columns of data (up to 63) in over 50 datasets. I am extracting only 3 columns of data that I need and writing it into a new .csv file. There are a few of my datasets that do not have the third desired column of data. But that's okay I can leave it blank (or insert another value like "-" or whatever). I don't want to open all my files to figure out which files have what. The error message I get when I try to extract data from a non-existent column is:

IndexError: list index out of range

Is there a loop that I can write to fix this? I'm really new to python, and in my head it seems easy but when I try to actually do it it's very difficult.

Thanks

A: 

Based on the error message, I'm guessing you have a list of lists that looks something like this (a gross simplification):

[[0,1,2,3],
[1,2,3,4,5],
[1,2,3],
[1,2,3]]

And you are trying to do the following:

for row in xrange(4): for col in xrange(4): #something else? print data[row][col]

And then you're getting your error because one of the values doesn't have an element at index 3:

+------------------------+
| Index: | 0 | 1 | 2 | 3 |
+------------------------+
|Value:  | 1 | 2 | 3 |  <----- No value at index 3
+--------------------+ 

Depending on where you're getting your data from originally, there are several different ways to accomplish what you're trying to accomplish.

If you provide sample I/O you'll get much better answers.

Wayne Werner
A: 

I assume you're doing something like:

for line in file:
    parts = line.split()
    blah = line[2]

And blah doesn't exist for some lines.

You can check the length of lists:

if len(parts) > 2:
    blah = line[2]
else:
    blah = "" # or whatever

Without any example code it's hard to be more precise, but this is probably a quick and easy fix for what you're doing.

wds
thanks for that!
Harper_C
A: 

Instead of looping through all your data before you start, you could just catch the Exception and handle it appropriately:

try:
    a = list[57]
except IndexError:
    a = '-'
Dave Webb