cgi - cleaning tabs and returns out of textarea

Steve Holden sholden at holdenweb.com
Mon Jan 28 10:30:25 EST 2002


"Glenn Stauffer" <java at dejazzd.com> wrote ...
>
> I have a cgi utility which I wrote to process form data and save key-value
> pairs in a database.  Since it is a generic utility, I need to handle any
> type of data and store it in a form that can be converted into a
> tab-delimited download.
>
> In many browsers, tabs and carriage returns can be embedded in a textarea
> field.
>
> I wrote this function to strip these characters:
>
> def clean(text, tab_width):
> text = text.strip() + ' '
> return text.expandtabs(tab_width)
>
> The problem I've run into is that the return/linefeed characters are
embedded
> within the value returned from the form and strip() won't work.  I wrote
> another function that tests each character and strips the carriage
> return/linefeeds, but I'm finding that  browsers often replace a return
with
> a carriage return/linefeed and I end up with two spaces in the text.
>
> I'm working on figuring out the best way to do this, but thought I'd send
to
> the list to see if someone could steer me in the right direction.
>
Had you thought about urllib.quote()? That would use %escapes to encode
tricky characters and keep them out of your hair.

>>> import urllib
>>> urllib.quote("""Tab\tCRLF\r\nand other stuff""")
'Tab%09CRLF%0d%0aand%20other%20stuff'

Than you can apply urllib.unquote() at an appropriate time.

regards
 Steve
--
Consulting, training, speaking: http://www.holdenweb.com/
Python Web Programming: http://pydish.holdenweb.com/pwp/








More information about the Python-list mailing list