simple string question
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: simple string question
"Chris Rebert" wrote in message news:[email protected]... > On Sun, Sep 6, 2009 at 10:29 PM, jwither 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)? > > There's probably a more general method covering all the escape > sequences, but for just \n: > > your_string = your_string.replace("\\n", "\n") > > Cheers, > Chris > -- > http://blog.rebertia.com Thanks! (the others are more likely to be errors than deliberate anyway) James Withers -- http://mail.python.org/mailman/listinfo/python-list
Re: simple string question
"ryles" wrote in message
news:b96be200-9762-4f92-bd0d-9be076bcd...@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.
--
http://mail.python.org/mailman/listinfo/python-list
