simple string question
niklas.norrthon at hotmail.com
Tue Sep 8 08:27:59 CEST 2009
On 8 Sep, 05:39, Steven D'Aprano
<ste... at REMOVE.THIS.cybersource.com.au> wrote:
> On Mon, 07 Sep 2009 01:54:09 -0700, Niklas Norrthon wrote:
> > Others have answered how to replace '\\n' with '\n'. For a more general
> > approach which will handle all string escape sequences allowed in python
> > (including '\xdd' and similar), python's eval can be used:
> eval can do so much more than handle escape sequences:
Yes, eval is really cool :-)
> quoted_string = ') or __import__("os").system("echo \'Pwn3d\';#rm -rf /"'
> print eval('str(%s)' % quoted_string)
> Every (bad) programmer should pass untrusted strings to eval as a quick
> and unsafe way to do trivial transformations.
It all depends on the origin of the strings of course.
I must admit that I didn't think of str.decode('string_escape') which
of course is the "correct" way to solve the problem (after inspecting
a sample of the input data to make sure it conforms to the
specification, and isn't rtf or some such).
I probably should decrease the volume of quick and dirty one time
hacks I produce...
More information about the Python-list