How to replace single quotes (')
with \'
in python?
Need to convert from
"{'fr': '', 'en': 'Title Edit 02'}"
to
"{\'fr\': \'\', \'en\': \'Changed\'}"
Try the following code:
s = "{'fr': '', 'en': 'Title Edit 02'}"
s= s.replace("'","\\'").strip("\\'")
print(s) # Or Do What you need with s
Output: {\'fr\': \'\', \'en\': \'Title Edit 02\'}
Explanation: Replace all ' with '. strip() can be omitted, just a fail safe.
Talk is cheap. Show you the code
str = "{'fr': '', 'en': 'Title Edit 02'}"
str1 = str.replace("\'", "\\\'")
print(str1)
OUT
"{\'fr\': \'\', \'en\': \'Title Edit 02\'}"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.