[英]How to replace single quotes (') with \' in python?
How to replace single quotes (')
with \'
in python?如何将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\'}
Output: {\'fr\': \'\', \'en\': \'Title Edit 02\'}
Explanation: Replace all ' with '.解释:将所有'替换为'。 strip() can be omitted, just a fail safe. strip() 可以省略,只是一个故障保险。
Talk is cheap.谈话很便宜。 Show you the code给你看代码
str = "{'fr': '', 'en': 'Title Edit 02'}"
str1 = str.replace("\'", "\\\'")
print(str1)
OUT出去
"{\'fr\': \'\', \'en\': \'Title Edit 02\'}"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.