- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
使用 lxml 库我已经对象化了一些元素(下面的示例代码)
config = objectify.Element("config")
gui = objectify.Element("gui")
color = objectify.Element("color")
gui.append(color)
config.append(gui)
config.gui.color.active = "red"
config.gui.color.readonly = "black"
config.gui.color.match = "grey"
结果是下面的结构
config
config.gui
config.gui.color
config.gui.color.active
config.gui.color.readonly
config.gui.color.match
我可以获得每个对象的完整路径
for element in config.iter():
print(element.getroottree().getpath(element))
路径元素由斜杠分隔,但这不是问题。我不知道如何只获取路径的父部分,以便我可以使用 setattr 更改给定元素的值
例如元素
config.gui.color.active
我要输入指令
setattr(config.gui.color, 'active', 'something')
但不知道如何获取完整路径的“父”部分。
最佳答案
您可以使用 getparent
函数获取元素的父元素。
for element in config.iter():
print("path:", element.getroottree().getpath(element))
if element.getparent() is not None:
print("parent-path:", element.getroottree().getpath(element.getparent()))
您也可以只删除元素路径本身的最后一部分。
for element in config.iter():
path = element.getroottree().getpath(element)
print("path:", path)
parts = path.split("/")
parent_path = "/".join(parts[:-1])
print("parent-path:", parent_path)
关于python - 如何获取 lxml.etree._ElementTree 对象的父路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53171562/
使用 lxml 库我已经对象化了一些元素(下面的示例代码) config = objectify.Element("config") gui = objectify.Element("gui") co
我在 lxml 库方面遇到了一个恼人的问题,并且不知道如何解决它。 我有一个 lxml.etree._ElementTree 树列表和一个属于这些树的 lxml.html.HtmlElement 列表
我正在运行 python 2.7.2我安装了 lxml 和 cssselect 我的代码是 from lxml import etree, html r = html.parse(start_url)
在过去的几天里,我一直在学习 lxml 的基础知识;特别是使用 lxml.html 来解析网站并创建内容的 ElementTree。理想情况下,我想保存返回的 ElementTree,这样我就可以加载
我有一个对象 arptree,我试图找到一个 ip 地址的 mac 地址,但失败了。 >>> arptree 当我尝试以下 xpath 时,它返回一个空列表 >>> arptree.xpath("d
>>> from lxml import objectify >>> from StringIO import StringIO >>> f = StringIO("data") >>> tree =
请帮助使 getpath() 返回 getpath() xpath 中的完整标记名称或找到解决方法 我正在尝试为 lxml.etree._ElementTree 中的元素生成 xpath。 Eleme
我是一名优秀的程序员,十分优秀!