Jonathan Karon wrote:
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

      if not line.strip(): continue  # no need for the len() check

    # ... existing code here ...

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(',')]

or, if not concerned about whitespace in the element headers:

  chems = infile.readline().strip().split(',')

~Ethan~
_______________________________________________
Portland mailing list
[email protected]
http://mail.python.org/mailman/listinfo/portland

Reply via email to