From: Douglas S. B. <db...@cs...> - 2008-05-24 13:04:04
|
> Stéphane, > > On Fri, 2008-05-23 at 23:51 -0700, Stéphane Charette wrote: >> Given the following python lines: >> >> self.write( '# DEBUG1="%s"\n' % label ) >> label = label.strip() >> self.write( '# DEBUG2="%s"\n' % label ) >> >> Why do I get the following output: >> >> # DEBUG1="\ntest aaa\ntest bbb\n\n" >> # DEBUG2="\ntest aaa\ntest bbb\n\n" >> >> I would have expected the left and right whitespace chars ("\n") to be >> stripped from the 2nd debug line. > > strip() does not affect newlines. > >> What python function should I be calling instead of strip()? > > replace('\n','') Of course that will remove the newlines in the middle, too, and doesn't deal with tabs. How about: def strip(s): start = 0 stop = -1 while start < len(s): if s[start] not in ['\n', '\t', ' ']: break start += 1 if start == len(s): return u"" while abs(stop) < len(s): if s[stop] not in ['\n', '\t', ' ']: break stop -= 1 return s[start:len(s) + stop + 1] I didn't test it with multi-byte unicode characters, but I think it would do the right thing. -Doug > Not a function, strictly speaking, but rather a method of the > unicode() objects. > > See "help(unicode)" for more details, > Alex > > ------------------------------------------------------------------------- > This SF.net email is sponsored by: Microsoft > Defy all challenges. Microsoft(R) Visual Studio 2008. > http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/_______________________________________________ > Gramps-devel mailing list > Gra...@li... > https://lists.sourceforge.net/lists/listinfo/gramps-devel > |