如何正确转义单引号和双引号 [英] How to properly escape single and double quotes
问题描述
我有一个lxml etree HTMLParser对象,我正在尝试使用它建立xpath来断言xpath,xpath的属性和该标记的文本.当标签的文本具有单引号(')或双引号()时,我遇到了一个问题,我用尽了所有选项.
I have a lxml etree HTMLParser object that I'm trying to build xpaths with to assert xpaths, attributes of the xpath and text of that tag. I ran into a problem when the text of the tag has either single-quotes(') or double-quotes(") and I've exhausted all my options.
这是我创建的示例对象
parser = etree.HTMLParser()
tree = etree.parse(StringIO(<html><body><p align="center">Here is my 'test' "string"</p></body></html>), parser)
这是代码段,然后是读入的变量的不同变体
Here is the snippet of code and then different variations of the variable being read in
def getXpath(self)
xpath += 'starts-with(., \'' + self.text + '\') and '
xpath += ('count(@*)=' + str(attrsCount) if self.exactMatch else "1=1") + ']'
self.text基本上是标记的预期文本,在这种情况下:这是我的测试"字符串"
self.text is basically the expected text of the tag, in this case: Here is my 'test' "string"
当我尝试使用HTMLParser对象的xpath方法时,这将失败
this fails when i try to use the xpath method of the HTMLParser object
tree.xpath(self.getXpath())
原因是因为它获取的xpath是这个'/html/body/p [starts-with(.,'这是我的'test'字符串"')和1 = 1]'
Reason is because the xpath that it gets is this '/html/body/p[starts-with(.,'Here is my 'test' "string"') and 1=1]'
如何正确地将self.text变量中的单引号和双引号转义?我试过三重引号,将self.text包裹在repr()中,或者做一个re.sub或string.replace,以'和'的\和'来转义
How can I properly escape the single and double quotes from the self.text variable? I've tried triple quoting, wrapping self.text in repr(), or doing a re.sub or string.replace escaping ' and " with \' and \"
推荐答案
根据我们在Wikipedia中看到的 和 w3学校,则您不应具有'
和"
在节点内容中,即使仅<
和&
被认为是严格非法的.应将它们替换为相应的预定义实体引用",分别为'
和"
.
According to what we can see in Wikipedia and w3 school, you should not have '
and "
in nodes content, even if only <
and &
are said to be stricly illegal. They should be replaced by corresponding "predefined entity references", that are '
and "
.
顺便说一句,我使用的Python解析器将透明地处理此问题:编写时将它们替换;阅读时,它们会被转换.
By the way, the Python parsers I use will take care of this transparently: when writing, they are replaced; when reading, they are converted.
重新阅读您的答案后,我用'
测试了一些东西,以此类推,在Python解释器中进行了测试.它将为您摆脱一切!
After a second reading of your answer, I tested some stuff with the '
and so on in Python interpreter. And it will escape everything for you!
>>> 'text {0}'.format('blabla "some" bla')
'text blabla "some" bla'
>>> 'ntsnts {0}'.format("ontsi'tns")
"ntsnts ontsi'tns"
>>> 'ntsnts {0}'.format("ontsi'tn' \"ntsis")
'ntsnts ontsi\'tn\' "ntsis'
因此我们可以看到Python正确地进行了转义.然后可以复制粘贴收到的错误消息(如果有)吗?
So we can see that Python escapes things correctly. Could you then copy-paste the error message you get (if any)?
这篇关于如何正确转义单引号和双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!