[portland] List Index Out Of Range
monk at netjunky.com
Wed Jul 18 20:46:03 CEST 2012
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:
> 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
> 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:
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.
> Portland mailing list
> Portland at python.org
More information about the Portland