Re: How to unescape a raw string?

2010-05-20 Thread Gary Herron

On 05/19/2010 08:34 PM, pyt...@bdurham.com wrote:

How can I unescape a raw string so that it behaves as a non-raw string?
For example, if I read the string \n\t A B C\ D E F \xa0 \u1234 
from a text file, how can I convert (unescape?) this string so that 
\n, \t, \, \x, and \u get converted to a newline, tab, double quote, 
hex encoded and unicode encoded chars?
I know I can do this explictly via a series of .replace() methods, but 
certainly there must be a built-in way to do this on a generic basis?

Thank you,
Malcolm



That question makes no sense -- a string is a string is a string in 
Python.   The syntax you use to specify the string (for instance, raw or 
not, hex characters or not, or one or another quote choice) is 
irrelevant and completely forgotten once the string is internal.


No need to use replace, the \n is already stored as a newline, and the 
\t as a tab and so on.Various methods of output may or may not 
convert those characters back into \n and \t and so on.  But that's a 
matter of output not internal storage.


So tell us what you're trying to accomplish -- and better also tell us 
Python2 or Python3?


Gary Herron

-- 
http://mail.python.org/mailman/listinfo/python-list


How to unescape a raw string?

2010-05-19 Thread python
How can I unescape a raw string so that it behaves as a non-raw
string?

For example, if I read the string \n\t A B C\ D E F \xa0
\u1234 from a text file, how can I convert (unescape?) this
string so that \n, \t, \, \x, and \u get converted to a newline,
tab, double quote, hex encoded and unicode encoded chars?

I know I can do this explictly via a series of .replace()
methods, but certainly there must be a built-in way to do this on
a generic basis?

Thank you,
Malcolm
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to unescape a raw string?

2010-05-19 Thread Chris Rebert
On Wed, May 19, 2010 at 8:34 PM,  pyt...@bdurham.com wrote:
 How can I unescape a raw string so that it behaves as a non-raw string?

That's not what the notion of raw strings in Python technically means,
but anyway...

 For example, if I read the string \n\t A B C\ D E F \xa0 \u1234

I'll assume you're quoting the file contents itself verbatim here,
rather than a Python string literal (which would require doubling up
on the backslashes).

 from a
 text file, how can I convert (unescape?) this string so that \n, \t, \, \x,
 and \u get converted to a newline, tab, double quote, hex encoded and
 unicode encoded chars?

 I know I can do this explictly via a series of .replace() methods, but
 certainly there must be a built-in way to do this on a generic basis?

new_string = your_string.decode('string_escape')

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to unescape a raw string?

2010-05-19 Thread python
Hi Chris,

 That's not what the notion of raw strings in Python technically means, but 
 anyway...

Agree - I was having a difficult time trying to describe my dilemma -
thanks for hanging in there with my rather awkward intro :)

 I'll assume you're quoting the file contents itself verbatim here, rather 
 than a Python string literal (which would require doubling up
on the backslashes).

Yes.

 new_string = your_string.decode('string_escape')

That's just the clue I needed. The actual decoder I needed turned out to
be 'unicode_escape' because of my embedded \u markup.

new_string = your_string.decode('unicode_escape')

Thank you for your help Chris!

Regards,
Malcolm
-- 
http://mail.python.org/mailman/listinfo/python-list