Hey all,

I have been reading some older posts on line/list manipulation, but don't quite understand exactly how to implement all of the tools that you guys are using (line.strip() for example) to get what I need. Essentially, I have a working program that iterates through data and outputs it into a file. If one of the lines in the file is blank, the program crashes. I want to know what simple snipet of code I can add to ensure that the program continues iterating. Even more awesome would be a way for the program to let me know at which points in the data blank lines appear. But I would be happy just having the program skip the blank lines.

Any suggestions?

Recommended Answers

All 3 Replies

Perhaps I can clarify my concern. Take this code, which claims to skip blank lines (and works) which I found online:

infile = open("text.txt","r")

for line in infile:
        if not line.strip():
                continue
        else:
                print line

What is this code saying? Take a line in the infile, and then I don't understand what "if not line.strip()" is really doing. How would you guys put that in laymen's terms? "if not line.strip()"

How would you guys put that in laymen's terms? if not line.strip()

I would translate it to: if empty line: I guess. Here's an example, basically this code is relying on the fact that Python considers an empty string, or '' to be a False, while a non-empty string is True. Let me demonstrate:

>>> empty_line = '\n'
>>> text_line = 'Lipsum\n'
>>> 
>>> empty_line.strip()
''
>>> text_line.strip()
'Lipsum'
>>> bool( empty_line.strip() )
False
>>> bool( text_line.strip() )
True
>>> if '':
...     print 'Empty!'
...     
>>> if not '':
...     print 'Empty!'
...     
Empty!
>>>

So as you can see, strip removes the new line character and any bounding white space (tabs, newline, spaces - on the far left and right of the string).

Since Python considers an empty line to be False, this code is waiting for a line that does not evaluate to False, before acting on it.

>>> lines = [ 'Not empty\n', '\n', '         Foobar\n' ]
>>> for line in lines:
...     print bool( line.strip() ), '=', line.strip()
...     
True = Not empty
False = 
True = Foobar
>>> for line in lines:
...     if line.strip():
...         print 'Found a line: %s' % line.strip()
...     else:
...         print 'Empty line!'
...     
Found a line: Not empty
Empty line!
Found a line: Foobar
>>> # Now repeat with 'not' to negate the True/False
>>> for line in lines:
...     if not line.strip():
...         print 'Empty line!'
...     else:
...         print 'Found a line: %s' % line.strip()
...     
Found a line: Not empty
Empty line!
Found a line: Foobar
>>>

Now the above example demonstrates the boolean evaluation of the string that is returned by strip() .

Hopefully that clears it up for you

What line.strip() is doing is removing whitespace from the ends of the string. If the line is completely empty, or it's only spaces and tabs, line.strip() will return an empty string '' which evaluates to false. This may not be the behavior you want. It all depends on whether you want a line with whitespace in it to count as a blank line.

jlm699's post is better than mine, just thought I'd try to clarify the line.strip() thing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.