Eat this one

Neil Schemenauer nas at python.ca
Tue Mar 27 13:02:57 EST 2001


Rikard Bosnjakovic wrote:
> I feel ashamed to show the code, but I didn't find any other solution :)

That's okay.  The code you have is pretty bad though (think about
the complexity of the algorithm).  Here is a solution which would
be much faster for large values of txt:

    import string

    _notrans = string.maketrans("", "") # don't translate any chars

    def remove_chars(txt, chars):
        return string.translate(txt, _notrans, chars)

    txt = cgi.escape(open("somefile").read())
    txt = remove_chars(txt, "\n<>&")

Cheers,

  Neil




More information about the Python-list mailing list