[portland] List Index Out Of Range

Jonathan Karon monk at netjunky.com
Wed Jul 18 20:46:03 CEST 2012


See below...

On Jul 18, 2012, at 11:27 AM, Rich Shepard wrote:

> On Wed, 18 Jul 2012, Jonathan Karon wrote:
> 
>> This version gives you one line per column:
> 
> Jonathan,
> 
>  Almost! It runs to completion (132K lines), but there's still a list index
> error thrown and the quantity associated with the last chemical is on a new
> line. It would be helpful for me to understand these two errors.
> 
>  Here's what python shows:
> 
> Traceback (most recent call last):
>  File "./wide-to-long.py", line 24, in <module>
>    timestamp = data[1]
> IndexError: list index out of range

This is likely happening because the last line of the file is blank. "data\ndata\n".split('\n') will return 3 values - 2 with the text "data" and one a string with 0 length

The simplest thing to do is this:

for line in infile.read().split('\n'):
    if not len(line.strip()): continue # ignore blank lines

    # ... existing code here ...

> 
>  And here are the last few lines of one site/date set and the beginning few
> lines of the next one:
> 
> D-1,2007-12-12,V,0.000
> D-1,2007-12-12,Zn
> ,11.400
> D-1,2008-03-15,Ag,-0.005
> D-1,2008-03-15,Al,-0.080

From http://docs.python.org/tutorial/inputoutput.html#methods-of-file-objects :
" f.readline() reads a single line from the file; a newline character (\n) is left at the end of the string "

The last element of your chems list will still contain that trailing linefeed. Try this when initially loading the chems list:

chems = [c.strip() for c in infile.readline().split(',')]


> 
>  Both the python error and the format issue must be simple but I still
> don't see the reasons.
> 
> Thanks,
> 
> Rich
> 
> _______________________________________________
> Portland mailing list
> Portland at python.org
> http://mail.python.org/mailman/listinfo/portland



More information about the Portland mailing list