-6

In the following code:

def data_from_file(fname, sep=';'):

    file_iter = open(fname, 'r')
    for line in file_iter:
        line = line.strip()
        if 0 == len(line): continue
        row = line.split(sep)
        try:
            leg = int(row[2])
        except ValueError:
            leg = "NONE"
        yield DATA(type=row[1], leg=leg, time=int(row[3]), id=row[0])

I am getting the error message:

in data_from_file
    leg = int(row[2])
IndexError: list index out of range

How can I fix this?

TwistedSim
  • 1,960
  • 9
  • 23
Maren
  • 63
  • 1
  • 6
  • And? Where is the question? – Guybrush Apr 27 '18 at 18:46
  • 1
    I suggest googling "python how to interpret common error messages" or just "python indexerror". It's easy to understand what this means and to take steps to solve the problem. Most of the code in the question is not related to your error, and the information we'd need to solve the error is not present. – Alex Hall Apr 27 '18 at 18:48
  • We need a [mcve]. – Aran-Fey Apr 27 '18 at 18:49
  • 1
    Make sure `line.split()` actually *produces* 3 or more fields. The code is mostly fine; you are just making assumptions about the contents of the file that are apparently false. – chepner Apr 27 '18 at 18:50

1 Answers1

0

Both to make your code more explicit about its intent and to ease debugging, I'd change your code slightly:

def data_from_file(fname, sep=";"):
    with open(fname) as file_iter:
        for line in file_iter:
            line = line.strip()
            if not line:
                continue
            try:
                id, type_, leg, time = line.split(sep)
            except ValueError:
                # raise ValueErr("Bad line: %s" % (line,))
                # print("Bad line, skipping: %s" % (line, )
            try:
                leg = int(leg):
            except ValueError:
                leg = "NONE"
            yield DATA(type_, leg, int(time), id)

Uncomment one of the lines in the first ValueError handler to either abort on a bad line or to skip it.

chepner
  • 497,756
  • 71
  • 530
  • 681