From: ryles on

> There's probably a more general method covering all the escape
> sequences, but for just \n:
>
> your_string = your_string.replace("\\n", "\n")

py> s = "hello\\r\\n"
py> s
'hello\\r\\n'
py> s.decode("string_escape")
'hello\r\n'
py>

From: Niklas Norrthon on
On 7 Sep, 07:29, "jwither" <jwit...(a)sxder4kmju.com> wrote:
> Given a string (read from a file) which contains raw escape sequences,
> (specifically, slash n), what is the best way to convert that to a parsed
> string, where the escape sequence has been replaced (specifically, by a
> NEWLINE token)?
>
> James Withers

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:

>>> quoted_string = "'hello\\nworld\\x21\\tand\\tgood\\040\\x47ood bye!'"
>>> print quoted_string
'hello\nworld\x21\tAnd\tgood\040\x47ood bye!'
>>> print eval('str(%s)' % quoted_string)
hello
world! And good Good bye!

If the string isn't quoted just enclosed it in quotes first:
>>> unquoted_string = 'hello\\nworld\\x21\\tand\\tgood\\040\\x47ood bye!'
>>> print unquoted_string
hello\nworld\x21\tAnd\tgood\040\x47ood bye!
>>> print eval('str("%s")' % unquoted_string)
hello
world! And good Good bye!

/Niklas Norrthon
From: D'Arcy J.M. Cain on
On Mon, 7 Sep 2009 15:29:23 +1000
"jwither" <jwither(a)sxder4kmju.com> wrote:
> Given a string (read from a file) which contains raw escape sequences,
> (specifically, slash n), what is the best way to convert that to a parsed
> string, where the escape sequence has been replaced (specifically, by a
> NEWLINE token)?

I don't know what your actual requirement is but maybe this fits:

exec("print '%s'" % x)

--
D'Arcy J.M. Cain <darcy(a)druid.net> | Democracy is three wolves
http://www.druid.net/darcy/ | and a sheep voting on
+1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.
From: Steven D'Aprano on
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:


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.

http://en.wikipedia.org/wiki/Code_injection



--
Steven
From: jwither on

"ryles" <rylesny(a)gmail.com> wrote in message
news:b96be200-9762-4f92-bd0d-9be076bcd786(a)y20g2000vbk.googlegroups.com...
>
>> There's probably a more general method covering all the escape
>> sequences, but for just \n:
>>
>> your_string = your_string.replace("\\n", "\n")
>
> py> s = "hello\\r\\n"
> py> s
> 'hello\\r\\n'
> py> s.decode("string_escape")
> 'hello\r\n'
> py>
>

Even though that's what I asked for, I'll stick with the "replace" for now.
But it's cool though: I can embed generic uni-code as well as simple escape
sequences!

Thanks,
James Withers.