Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: escaping

Reply
Thread Tools

Re: escaping

 
 
Steven D'Aprano
Guest
Posts: n/a
 
      04-16-2012
On Sun, 15 Apr 2012 23:07:36 +0200, Kiuhnm wrote:

> This is the behavior I need:
> path = path.replace('\\', '\\\\')
> msg = ". {} .. '{}' .. {} .".format(a, path, b)
> Is there a better way?



This works for me:

>>> a = "spam"
>>> b = "ham"
>>> path = r"C:\a\b\c\d\e.txt"
>>> msg = ". %s .. %r .. %s ." % (a, path, b)
>>> print msg

.. spam .. 'C:\\a\\b\\c\\d\\e.txt' .. ham .



Another possibility:

>>> print path.encode('string_escape')

C:\\a\\b\\c\\d\\e.txt


--
Steven
 
Reply With Quote
 
 
 
 
Jon Clements
Guest
Posts: n/a
 
      04-16-2012
On Monday, 16 April 2012 11:03:31 UTC+1, Kiuhnm wrote:
> On 4/16/2012 4:42, Steven D'Aprano wrote:
> > On Sun, 15 Apr 2012 23:07:36 +0200, Kiuhnm wrote:
> >
> >> This is the behavior I need:
> >> path = path.replace('\\', '\\\\')
> >> msg = ". {} .. '{}' .. {} .".format(a, path, b)
> >> Is there a better way?

> >
> >
> > This works for me:
> >
> >>>> a = "spam"
> >>>> b = "ham"
> >>>> path = r"C:\a\b\c\d\e.txt"
> >>>> msg = ". %s .. %r .. %s ." % (a, path, b)
> >>>> print msg

> > . spam .. 'C:\\a\\b\\c\\d\\e.txt' .. ham .

>
> I like this one. Since I read somewhere that 'format' is preferred over
> '%', I was focusing on 'format' and I didn't think of '%'.
> Anyway, it's odd that 'format' doesn't offer something similar.
>
> Kiuhnm


If you look at http://docs.python.org/library/strin...-string-syntax

you'll notice the equiv. of %r is {!r}
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      04-16-2012
On Mon, 16 Apr 2012 12:03:31 +0200, Kiuhnm wrote:

> On 4/16/2012 4:42, Steven D'Aprano wrote:
>> On Sun, 15 Apr 2012 23:07:36 +0200, Kiuhnm wrote:
>>
>>> This is the behavior I need:
>>> path = path.replace('\\', '\\\\')
>>> msg = ". {} .. '{}' .. {} .".format(a, path, b)
>>> Is there a better way?

>>
>>
>> This works for me:
>>
>>>>> a = "spam"
>>>>> b = "ham"
>>>>> path = r"C:\a\b\c\d\e.txt"
>>>>> msg = ". %s .. %r .. %s ." % (a, path, b) print msg

>> . spam .. 'C:\\a\\b\\c\\d\\e.txt' .. ham .

>
> I like this one. Since I read somewhere that 'format' is preferred over
> '%', I was focusing on 'format' and I didn't think of '%'. Anyway, it's
> odd that 'format' doesn't offer something similar.


But it does. Before making assumptions about what format does and does
not offer, remember that the Fine Manual is your friend.

py> print "%r" % r'C:\a\b\c\d.txt'
'C:\\a\\b\\c\\d.txt'
py> print "{0!r}".format(r'C:\a\b\c\d.txt')
'C:\\a\\b\\c\\d.txt'



--
Steven
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      04-16-2012
On Mon, 16 Apr 2012 23:12:52 +0000, Steven D'Aprano wrote:

> But it does. Before making assumptions about what format does and does
> not offer, remember that the Fine Manual is your friend.


Ha, it would help if I remembered to link to the manual... sorry about
that.

http://docs.python.org/library/strin...-mini-language



--
Steven
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      04-16-2012
On Mon, 16 Apr 2012 23:27:04 +0000, Steven D'Aprano wrote:

> On Mon, 16 Apr 2012 23:12:52 +0000, Steven D'Aprano wrote:
>
>> But it does. Before making assumptions about what format does and does
>> not offer, remember that the Fine Manual is your friend.

>
> Ha, it would help if I remembered to link to the manual... sorry about
> that.
>
> http://docs.python.org/library/strin...-mini-language



Okay, this is getting to be a record, even for me. Wrong link.

http://docs.python.org/library/strin...-string-syntax


(right page, wrong anchor)

I'll just slink away now...



--
Steven
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
escaping characters Mike P ASP .Net 1 03-29-2006 03:09 PM
Escaping a CrLf in JavaScript George Durzi ASP .Net 0 07-06-2004 05:03 AM
Re: Escaping a URL in XSL/T Teemu Keiski ASP .Net 0 05-13-2004 04:59 PM
Re: Escaping a URL in XSL/T Swanand Mokashi ASP .Net 0 05-13-2004 04:58 PM
Escaping ' in Javascript ? Ben ASP .Net 4 11-10-2003 06:22 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57