[Tutor] Index Out of Range?List

Mats Wichmann mats at wichmann.us
Thu May 4 13:24:16 EDT 2017

>         atm_chg.append(float( line.split()[-1] ))
> np.asarray({atm_chg})
> Execution still generates the errors:
> runfile('/home/comp/Apps/Python/Testing/ReadFile_2.py',
> wdir='/home/comp/Apps/Python/Testing')

that means you have a blank line it's reading, the result of splitting
an empty line is an empty list, which you can't index since it has no

$ python
Python 2.7.13 (default, Jan 12 2017, 17:59:37)
[GCC 6.3.1 20161221 (Red Hat 6.3.1-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> ''.split()
>>> ''.split()[-1]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range

you're going to need to do a little input validation, always a good idea
anyway.  that is inside this loop:

      for line in f.readlines():
          atm_chg.append(float( line.split()[-1] ))

check there's something usable in line before you split-and-use it.

More information about the Tutor mailing list