more on unescaping escapes

bvdp bob at mellowood.ca
Mon Feb 23 19:26:29 EST 2009


Tim Wintle wrote:
> On Mon, 2009-02-23 at 17:00 -0700, bvdp wrote:
>> Let's see if this makes sense:
>>
>>  >>> a='c:\\Program Files\\test'
>>  >>> a.decode('string-escape')
>> 'c:\\Program Files\test'
> 
> Hint: try running
> 
>>>> print a
> 
> and see what's written - I think that the interpreter adds extra "\"
> characters to escape things and make things more easy to read.
> 
> i.e. 
> 
>>>> a = "c:\\test\\t"
>>>> a
> 'c:\\test\\t'
>>>> print a
> c:\test\t
> 
> so when it displays strings in the interpreter it includes escape
> characters, when it is printed though the output is straight to stdout
> and isn't escaped.
> 
> Hope that helps,
> 
> Tim Wintle

Not sure if it's more clear or not :)

 >>> a="c:\\Program\x20Files\\test"
 >>> a
'c:\\Program Files\\test'
 >>> print a
c:\Program Files\test

Which is all fine. And I didn't need to use decode().

So, in this case I'm assuming that the interpreter is converting the 
escapes on assignment. And, in this case the string has single \s in it.

So far this is making sense. So, when I do a decode() the \t is 
converted to a tab, etc.

I think my "problem" may be in viewing an assignment like that above as 
opposed to reading a line from a file with '\'s and '\x20's in it. In 
this case the assignment doesn't do the conversion (and that is a good 
thing!). Now, when I go to save the variable with the escapes I use 
decode() and it works.

Just a matter of consistantly using a method I suppose. Still, confusing 
between my program reading data from a file and doing testing of strings 
at the interpreter. Best to do one or the other, not both.








More information about the Python-list mailing list