0

I am parsing a list of data from a file I am reading in, and every line ends with a '\n' character. When I try to stop at the end of the line, my while does not stop at the newline character.

while parsed_data[i] is not '\n':
    if parsed_data[i] is not '':
        temp_data.append(parsed_data[i])
    i += 1

The link to a screenshot of how the data I'm looping through is here: http://i.imgur.com/2ycDDDL.png

When the loop gets to the 11th element the loop does not exit, causing it to run for another round and thus increment past the bounds of the list.

Aaron
  • 2,367
  • 3
  • 25
  • 33
  • This may not be related to your problem, but consider using `==` and `!=` to compare strings, since `is` checks strict referential identity, and may have surprising results. – Kevin Jun 17 '14 at 16:38
  • short answer: „is” check the class, == and != the content – cox Jun 17 '14 at 16:44
  • @cox, not sure what you mean by that. `a is b` can return False even when they have the same class. Perhaps you're thinking of `isinstance`? – Kevin Jun 17 '14 at 16:46
  • No, the „is” check the reference, to be exact, not the value itself – cox Jun 17 '14 at 16:48

1 Answers1

1

is does not always return True for two strings that are identical. Example:

>>> a = "\n"
>>> b = "\n"
>>> a is b
False
>>> a == b
True

Replace the is nots in your code with !=.

Kevin
  • 74,910
  • 12
  • 133
  • 166