Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python xpath类型错误:';lxml.etree._ElementTree';对象是不可编辑的_Python_Xml_Xml Parsing - Fatal编程技术网

Python xpath类型错误:';lxml.etree._ElementTree';对象是不可编辑的

Python xpath类型错误:';lxml.etree._ElementTree';对象是不可编辑的,python,xml,xml-parsing,Python,Xml,Xml Parsing,尽可能简单: <state name = "foo1> <foobar item1="something1" item2="somethingelse1" item3="usefulitem1"/> <foobar item1="something2" item2="somethingelse2" item3="usefulitem

尽可能简单:

<state name = "foo1>
    <foobar item1="something1" item2="somethingelse1" item3="usefulitem1"/>
    <foobar item1="something2" item2="somethingelse2" item3="usefulitem2"/>
<state name = "foo2">
...
root = lxml.etree.parse(fileName)
path = "./*[contains(text(),'useful')]"
someElement = root.xpath(path)

我实际上认为您可以从树对象创建XPath。呵呵。但你绝对不能重复它。为此,需要获取根(元素)

从文档中:


获取此树的根元素

所以你的代码应该是这样的

<state name = "foo1>
    <foobar item1="something1" item2="somethingelse1" item3="usefulitem1"/>
    <foobar item1="something2" item2="somethingelse2" item3="usefulitem2"/>
<state name = "foo2">
...

棘手的是,当您使用
.fromstring
而不是文件时,您会自动拥有根元素,而不是
元素树

仍然不是文件。“XML”的格式甚至不是很好。对不起,当我打开我的XML时,我看到的就是这个。我不知道我还能得到多少,但我同意你的xml文件看起来很奇怪。在这个问题中,我本以为会出现类似的情况:
tree.getroot()
不是必需的。
xpath()
方法在
Element
ElementTree
对象上都可用。