.lstrip().rstrip() -> .strip() and other String/Unicode suggestions

Uche Ogbuji uche at ogbuji.net
Mon Mar 15 05:02:30 CET 2004


nikita_raja at yahoo.com (Sam Smith) wrote in message news:<b19beb92.0403051302.456e024f at posting.google.com>...
>     def characters(self, content):
>         self.content = self.content.lstrip().rstrip() + " " + content

FYI, you can just use the following, which is equivalent:

        self.content = self.content.strip() + u" " + content

Notice how I also maintain the Unicode object character of
self.content

Overall, it would be much more efficient to use cStringIO or even
treat self.content as a list and use

f.write(u' '.join(ReadXML.content))

Watch out for encoding issues on write if you do (rightly) stick to
Unicode objects: use a codec-wrapped output stream.

Concatenating strings with + is notoriously wasteful.

See, for example:

http://mail.python.org/pipermail/xml-sig/2000-August/003198.html

--Uche
http://uche.ogbuji.net



More information about the Python-list mailing list