Python 用转义引号替换字符串中的所有引号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18886596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Replace all quotes in a string with escaped quotes?
提问by mix
Given a string in python, such as:
给定python中的一个字符串,例如:
s = 'This sentence has some "quotes" in it\n'
I want to create a new copy of that string with any quotes escaped (for further use in Javascript). So, for example, what I want is to produce this:
我想创建一个该字符串的新副本,其中的任何引号都被转义了(以便在 Javascript 中进一步使用)。所以,例如,我想要的是产生这个:
'This sentence has some \"quotes\" in it\n'
I tried using replace()
, such as:
我尝试使用replace()
,例如:
s.replace('"', '\"')
but that returns the same string. So then I tried this:
但它返回相同的字符串。然后我尝试了这个:
s.replace('"', '\"')
but that returns double-escaped quotes, such as:
但这会返回双转义引号,例如:
'This sentence has some \"quotes\" in it.\n'
How to replace "
with \"
?
如何更换"
使用\"
?
UPDATE:
更新:
I need as output from this copyable text that shows both the quotes and the newlines as escaped. In other words, I want to be able to copy:
我需要这个可复制文本的输出,将引号和换行符都显示为转义。换句话说,我希望能够复制:
'This sentence has some \"quotes\" in it.\n'
If I use the raw string and print
the result I get the correctly escaped quote, but the escaped newline doesn't print. If I don't use print
then I get my newlines but double-escaped quotes. How can I create a string I can copy that shows both newline and quote escaped?
如果我使用原始字符串和print
结果,我会得到正确转义的引号,但不会打印转义的换行符。如果我不使用,print
那么我会得到换行符,但会得到双转义引号。如何创建一个我可以复制的字符串来显示换行符和引号转义?
采纳答案by Yussuf S
Hi usually when working with Javascript I use the json module provided by Python. It will escape the string as well as a bunch of other things as user2357112 has pointed out.
嗨,通常在使用 Javascript 时,我使用 Python 提供的 json 模块。正如 user2357112 指出的那样,它将转义字符串以及其他一些东西。
import json
string = 'This sentence has some "quotes" in it\n'
json.dumps(string) #gives you '"This sentence has some \"quotes\" in it\n"'
回答by ?s??o?
Your last attempt was working as you expected it to. The double backslashes you see are simply a way of displaying the single backslashes that are actually in the string. You can verify this by checking the length of the result with len()
.
您的最后一次尝试按预期进行。您看到的双反斜杠只是显示字符串中实际单反斜杠的一种方式。您可以通过检查结果的长度来验证这一点len()
。
For details on the double backslash thing, see: __repr__()
有关双反斜杠的详细信息,请参阅: __repr__()
UPDATE:
更新:
In response to your edited question, how about one of these?
回答您编辑的问题,其中之一怎么样?
print repr(s).replace('"', '\"')
print s.encode('string-escape').replace('"', '\"')
Or for python 3:
或者对于python 3:
print(s.encode('unicode-escape').replace(b'"', b'\"'))
回答by Tim Peters
Your second attempt is correct, but you're getting confused by the difference between the repr
and the str
of a string. A more idiomatic way of doing your second way is to use "raw strings":
您的第二次尝试是正确的,但是您对字符串的repr
和之间的区别感到困惑str
。第二种更惯用的方法是使用“原始字符串”:
>>> s = 'This sentence has some "quotes" in it\n'
>>> print s
This sentence has some "quotes" in it
>>> print s.replace('"', r'\"') # raw string used here
This sentence has some \"quotes\" in it
>>> s.replace('"', r'\"')
'This sentence has some \"quotes\" in it\n'
Raw strings are WYSIWYG: backslashes in a raw string are just another character. It is - as you've discovered - easy to get confused otherwise ;-)
原始字符串是所见即所得:原始字符串中的反斜杠只是另一个字符。它是 - 正如你所发现的 - 否则很容易混淆;-)
Printing the string (the 2nd-last output above) shows that it contains the characters you want now.
打印字符串(上面的倒数第二个输出)表明它包含您现在想要的字符。
Without print
(the last output above), Python implicitly applies repr()
to the value before displaying it. The result is a string that would produce the original if Python were to evaluate it. That's why the backlashes are doubled in the last line. They're not in the string, but are needed so that if Python were to evaluate it each \\
would become one \
in the result.
如果没有print
(上面的最后一个输出),Python 会repr()
在显示之前隐式地应用于该值。结果是一个字符串,如果 Python 对其进行评估,它将生成原始字符串。这就是为什么在最后一行中反冲加倍的原因。它们不在字符串中,但需要这样,如果 Python 要评估它,每个都\\
将成为\
结果中的一个。