在python中禁用自动转义

时间:2012-06-20 12:30:23

标签: python

当从包含已经转义的文本的基于Web的文档执行简单的open-read命令时,python将\添加到转义\

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

如何禁用此行为?

更新

可以复制,例如

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

或在我的情况下

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

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

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

Python只是为了显示目的而这样做,如果你在解释器中鬼混,那就是它。

换句话说,只有在明确或隐含地使用repr()时才会这样做。

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

答案 2 :(得分:0)

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