more on unescaping escapes
MRAB
google at mrabarnett.plus.com
Mon Feb 23 17:36:41 EST 2009
bvdp wrote:
>
> So, we think something is working and send of a bug fix to our client :)
>
> I'm not sure I understand this at all and wonder if there is bug?
>
> >>> a="c:\\Program\x20Files\\test"
> >>> a
> 'c:\\Program Files\\test'
>
> so far, so good.
>
> >>> a.decode("string-escape")
> 'c:\\Program Files\test'
>
> Umm, not so good? The \\ before the P is okay, but the \\t is change to \t
>
Decoding changes "\\x20" to "\x20", which is the same as " ", a space.
Decoding changes "\\t" to "\t", which is a tab.
Decoding _doesn't_ change "\\P" to "\P" because that's not a valid
escape sequence.
> and
>
> >>> print a.decode("string-escape")
> c:\Program Files est
>
> Now, decode() converted the \\t to a \t and print expanded the \t to a tab.
>
\t is already a tab.
> I would have thought that the \\t would have the same result as the \\P ???
>
> Obviously my brain is missing something (hopefully obvious).
>
Before storing the string (writing it to the file), encode it and then
replace " " with "\\x20":
C:\Program Files\test
becomes:
C:\Program Files\test
and then:
C:\\Program\x20Files\\test
After fetching the string (reading it from the file), decode it:
C:\\Program\x20Files\\test
becomes:
C:\Program Files\test
More information about the Python-list
mailing list