User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 391,572 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,835 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser:
Views: 116 | Replies: 3
Reply
Join Date: Aug 2008
Posts: 8
Reputation: dmpop is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
dmpop dmpop is offline Offline
Newbie Poster

Convert " to \" and ' to \'

  #1  
12 Days Ago
Hello,

Yet another silly question from yours truly. I need to add escape characters to " and ', so that "monkey's cool" becomes \"monkey\'s cool\". My primitive solution to the problem is as follows:

str1=raw_input("String:")
str1=str1.replace("\'", "\\\'")
str1=str1.replace("\"", "\\\"")

But I bet there is a more efficient and elegant solution to this.

Thank you!

Kind regards,
Dmitri
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2008
Posts: 6
Reputation: griefers is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 2
griefers griefers is offline Offline
Newbie Poster

Re: Convert " to \" and ' to \'

  #2  
12 Days Ago
That seems concise and simple to me.
Reply With Quote  
Join Date: Jul 2008
Posts: 82
Reputation: Gribouillis is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 10
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Junior Poster in Training

Re: Convert " to \" and ' to \'

  #3  
12 Days Ago
An extreme solution: a class to escape any set of characters (reuse freely)
  1. #!/usr/bin/env python
  2. # escaper.py
  3. # a class to escape characters in strings
  4. import re
  5.  
  6. class Escaper(object):
  7. def __init__(self, chars):
  8. self.pat = re.compile("[%s]" % re.escape(chars))
  9. def __call__(self, data):
  10. return self.pat.sub(lambda c: "\\"+c.group(0), data)
  11.  
  12. if __name__ == "__main__":
  13. esc = Escaper("\"'")
  14. esc2 = Escaper("oy ")
  15. mystring = '"monkey' + "'s" + ' cool"'
  16. print mystring
  17. print esc(mystring)
  18. print esc2(mystring)

The output is
"monkey's cool"
\"monkey\'s cool\"
"m\onke\y's\ c\o\ol"
Last edited by Gribouillis : 12 Days Ago at 11:48 am.
Reply With Quote  
Join Date: Aug 2008
Posts: 8
Reputation: dmpop is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
dmpop dmpop is offline Offline
Newbie Poster

Re: Convert " to \" and ' to \'

  #4  
12 Days Ago
Thanks, guys!

Kind regards,
Dmitri
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Python Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Python Forum

All times are GMT -4. The time now is 10:27 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC