Is there a function to remove escape characters from a string ?

John Machin sjmachin at lexicon.net
Thu Dec 25 07:45:15 EST 2008


On Dec 25, 9:00 pm, Stef Mientki <stef.mien... at gmail.com> wrote:
> hello,
>
> Is there a function to remove escape characters from a string ?
> (preferable all escape characters except "\n").

"\n" is not what most people would call an escape character. The "\"
is what most people would call an escape character when it is used in
a manner like in a Python non-raw string (e.g. "1\tStef\r\n2\tJames\r
\n").

Assuming (as James has done) that you meant you want to remove all but
"truly visible ASCII characters, plus newline", I'd have to ask: Are
you sure?? Do you really want to throw away tabs, when they might be
separating fields, as in the above example?

Let's start at the beginning:

Python 2.x or 3.x?
Type of your data objects is str/bytes or unicode/str or both?
If str/bytes, what encoding(s)?
What exactly are these "escape characters"?
Are you sure that you need to remove them all i.e. you don't want to
replace some with other characters?

HTH,
John



More information about the Python-list mailing list