Why do I require an "elif" statement here?

danielx danielwong at berkeley.edu
Sun Aug 6 12:34:41 EDT 2006


I'm surprised no one has mentioned neat-er, more pythonic ways of doing
this. I'm also surprised no one mentioned regular expressions. Regular
expressions are really powerful for searching and manipulating text.
Here is where I learned most of the stuff I know about regular
expressions:

http://www.amk.ca/python/howto/regex/

I think this howto does a pretty good job, and doesn't take too long to
read.

Anyway, here's my solution, which does Not use regular expressions:

def reindent(line):
    ## we use slicing, because we don't know how long line is
    head = line[:OLD_INDENT]
    tail = line[OLD_INDENT:]
    ## if line starts with Exactly so many spaces...
    if head == whitespace*OLD_INDENT and not tail.startswith(' '):
        return whitespace*NEW_INDENT + tail
    else: return line    # our default

emptyString = ""
lines = input.readlines()
reindented = [reindent(ln) for ln in lines]
output.write( emptyString.join(reindented) )

I'll bet you could put that all on one line using lambda instead of a
def, but this is Python, and we like clarity ;).

A regular expression could have taken the place of our reindent
function, but I'll leave that up to you to explore ;).

Jim wrote:
> Good stuff!
> Since I'm only interested in spaces being my only whitespace it makes
> sense for me to use "line.lstrip(whitespace)" in my script, thus
> eliminating the "elif char == '\n':" statement.
> Thanks,
> Jim
>
> Tim Chase wrote:
> > > 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