[Newbie]Trouble with string method

Robert Brewer fumanchu at amor.org
Fri Mar 26 16:23:02 EST 2004


Nathan Johns wrote:
> Thanks. I thought I would need to break up the list, just 
> didn't know how. 
> BTW, not in school anymore :P, just trying to do a little 
> extra at work, 
> engineers are such busy people.
> 
> What I want is to sort and lowercase everything BUT comments. 
> For example, a 
> piece of the file might look like this:
> 
> foo at bar.com                          #ABC
> FOO at BAR.COM                       #DEF
> Foo at Bar.Com                         #HIJ

I assume the first # in any given line means the rest of the line is a
comment (even if there are further #-signs in that line)? If so, use
split to handle each line:


try:
    body, comment = line.split("#", 1)
except ValueError:
    body, comment = line, ''
body = body.lower()
outfile.write(body)
if comment:
    outfile.write(comment)
outfile.write('\n')

Don't bother adding body and comment back together with a '+'; that
would just waste resources.


Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org




More information about the Python-list mailing list