-1

当从包含已转义文本的基于 Web 的文档中执行简单的打开读取命令时,python 添加\到转义\

href=\"http:\/\/
into
href=\\"http:\\/\\/

如何禁用此行为?

更新

例如可以复制

a= 'href=\"http:\/\/'

或者在我的情况下

我只是做了一个 open() 然后用 mechanize 读取。

那么我如何告诉 python 字符串已经被转义了呢?

4

3 回答 3

4

Python 没有改变任何东西。看下面:

>>> a= 'href=\"http:\/\/'
>>> a
'href="http:\\/\\/' # the str() method is called
>>> repr(a)
'\'href="http:\\\\/\\\\/\'' # repr() is meant to be how the object can be "read" back, or provide detailed information
>>> str(a)
'href="http:\\/\\/' # see first example
>>> print a
href="http:\/\/ # any conversion etc... is not performed, ie, you get your original string printed
于 2012-06-20T12:50:37.423 回答
1

Python 这样做只是为了显示目的,并且仅当您在解释器中鬼混时才这样做。

换句话说,它只有在repr()显式或隐式使用时才会这样做。

它显示它们,但不使用它们。他们并不真的在那里。

于 2012-06-20T12:57:16.760 回答
0

您不需要转义正斜杠。因此,您的输入字符串有问题。python 输出中的双反斜杠正是解释器表示单个反斜杠的方式,如果您打印它,“\\”将显示为一个。

于 2012-06-20T12:54:34.103 回答