Why do I require an "elif" statement here?
Tim Chase
python.list at tim.thechases.com
Sat Aug 5 17:31:19 EDT 2006
> Hard to believe that lstrip() produces an empty string on lines with
> just spaces and doesn't remove the '\n' with lines that have
> characters.
It's easy to understand that lstrip() is doing exactly what it's
supposed to. It evaluates from the left of your string,
discarding whitespace (spaces, tabs, and cr/lf characters) until
it hits a non-whitespace character or the end of the string.
When there's no non-whitespace, it returns an empty string.
If you wanted to remove the \n from the right of lines, there was
an earlier discussion on the list where someone (Bruno?) and I
went back and forth and I think we finally decided that the
"best" solution was
s.rstrip('\n')
which had the fewest side-effects.
However, when you use the output.write() method, you'd then have
to add the \n back in to make sure it ended up in the output stream.
If you wanted to continue to use lstrip(), you could also just
ensure that you're only stripping spaces (chr(0x20)) by using
s.lstrip(' ')
This would leave \t and \n characters unmolested.
More info can be found at
>>> help("".lstrip)
>>> help("".rstrip)
>>> help("".strip)
Hope this helps,
-tkc
More information about the Python-list
mailing list