python - 如何在 python 中转义字符串以用作 xpath 键
问题描述
我在python中遇到了这个关键错误
KeyError: ("./note/[@player='Ba'g']",)
使用时
player = "Ba'g"
xpathstring = "./note/[@player='{}']".format(player.replace("'","\'"))
p = xmltree.find(xpathstring)
所以我想知道如何转义字符串,以便我可以在 xmltree 中用作 xpath 搜索字符串
解决方案
当前,您正在将字符串传递"./note/[@player='Ba'g']"
给 find 函数。
似乎没有办法正确转义 xpath 中的单引号。
但是,您可以从单引号切换到双引号。尝试:
xpathstring = "./note/[@player=\"{}\"]".format(player)
这是链接问题中建议的解决方案。
推荐阅读
- hibernate - Spring Data JPA / Hibernate有条件地获取孩子
- elasticsearch - 如何过滤字典中的特定值?
- sas - Proc 报告 - 调用定义以更改 GROUPING 变量下第二行的格式?
- node.js - 我无法在 node.js 中使用 mongo atlas 将数据提取到另一个页面总是显示错误消息无法读取函数的属性
- python - TensorFlow 2:在一次调用 TensorFlow 时获取多个张量值
- powerbi - 通过列名过滤 Power BI 中的绘图
- mysql - 在 NEW 中删除表
- shell - 如何使用 plink 移动带日期的文件
- java - RServe:到主进程的控制管道已关闭/损坏
- salesforce - 使用服务器到服务器从 Salesforce 调用时,来自 Google 日历 API 的禁止 403 响应