为什么这段代码不能删除Python中的反斜杠?

2024-04-26 17:35:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个字符串格式的代码块。它的变量名是我的\字符串,如下所示:

'<div class="section page-centered"><div><b style="font-size: 24px">About 
Us</b></div><div>At <a href="https://close.com/" class="postings- 
link">Close</a>, we\'re building the sales
communication platform of the future. With our roots as the very first 
sales CRM to include built-in calling, we\'re leading the industry toward 
eliminating manual processes and helping compa
nies to close more deals (faster). Since our founding in 2013, we\'ve 
grown to become a profitable, 100% globally distributed team of ~33 high- 
performing, happy people that are dedicated to b
uilding a product our customers love. </div>'

我想把所有东西都放在那块,除了反斜杠。我在这里看到了很多问题和答案

new_string = my_string.replace("\\", "")

但我得到的结果是一样的。我做错什么了?你知道吗


Tags: oftheto字符串代码indivre
2条回答

反斜杠实际上是在转义单引号。如果我打印字符串,我会得到:

print('<div class ... </div>')

<div class="section page-centered"><div><b style="font-size: 24px">About Us</b></div><div>At <a href="https://close.com/" class="postings- link">Close</a>, we're building the salescommunication platform of the future. With our roots as the very first sales CRM to include built-in calling, we're leading the industry toward eliminating manual processes and helping companies to close more deals (faster). Since our founding in 2013, we've grown to become a profitable, 100% globally distributed team of ~33 high- performing, happy people that are dedicated to building a product our customers love. </div>

我不太清楚为什么它在某些机器上工作,但至少在我的机器上,有这样一个问题:

  • 紧随其后的反冲不是真正的反冲,除非是双重反冲:

作为

back_lash="hi\myname\is\backlash"
print(back_lash)

这将输出:

hi\myname\iacklash

在IDE中更容易看到它的颜色。你知道吗

Example

所以你可以看到,这是一个很特别的角色。和你在替补时做的一样,有两次反冲

正如在here中所说的,很难找到反冲。反斜杠(“\”)字符用于转义具有特殊含义的字符,例如换行符、反斜杠本身或引号字符。 可以使用前缀r或r:

字符串文本可以选择前缀字母“r”或“r”;这类字符串称为原始字符串,并使用不同的规则来解释反斜杠转义序列。你知道吗

r"Some string with \ backslash"

看看this,他们提到:

You need to escape your backslash by preceding it with, yes, another backslashThe \ character is called an escape character, which interprets the character following it differently. For example, n by itself is simply a letter, but when you precede it with a backslash, it becomes \n, which is the newline character.

正如您可能猜到的,\也需要转义,这样它就不会像转义字符那样工作。你必须。。。从本质上说,逃离。你知道吗

相关问题 更多 >