Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.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 将子元素添加到子元素lxml_Python_Xml_Lxml - Fatal编程技术网

Python 将子元素添加到子元素lxml

Python 将子元素添加到子元素lxml,python,xml,lxml,Python,Xml,Lxml,我使用lxml试图输出以下xml代码: <annotation> <folder>images</folder> <filename>IMG_8111.JPG</filename> <size> <width>400</width> <height>400</height> </size> &l

我使用lxml试图输出以下xml代码:

<annotation>
    <folder>images</folder>
    <filename>IMG_8111.JPG</filename>
    <size>
        <width>400</width>
        <height>400</height>
    </size>
    <segmented>0</segmented>
    <object>
        <name>Bottle</name>
        <bndbox>
            <xmin>16</xmin>
            <ymin>71</ymin>
            <xmax>390</xmax>
            <ymax>323</ymax>
        </bndbox>
    </object>
</annotation>
但它会抛出以下错误:

 etree.SubElement(size, "width").text = "Child 4"
TypeError: Argument '_parent' has incorrect type (expected lxml.etree._Element, got str)

请帮我解决我做错了什么以及如何继续

您的变量大小具有类型字符串,因为它具有“child 3”值。你应该做:

size = etree.SubElement(root,"size")
size.text = "child 3" 
etree.SubElement(size, "width").text="child 4"

您的变量大小具有类型字符串,因为它具有“child 3”值。你应该做:

size = etree.SubElement(root,"size")
size.text = "child 3" 
etree.SubElement(size, "width").text="child 4"

为了使
size=etree.SubElement(root,“size”).text=“Child 3”
按照您的预期进行计算,必须将其解释为:

(size = etree.SubElement(root, "size")).text = "Child 3"
在Python中,不能在表达式中执行赋值。相反,Python解释这一点的方式是:

size = "Child 3"
etree.SubElement(root, "size").text = "Child 3"
您可以使用两行代码重写代码,以获得所需的结果:

size = etree.SubElement(root, "size")
size.text = "Child 3"

在浏览了
lxml
API之后,它似乎不是一种在一行中创建元素和为
text
属性赋值的方法。

为了使
size=etree.SubElement(root,“size”).text=“Child 3”
按预期进行计算,必须将其解释为:

(size = etree.SubElement(root, "size")).text = "Child 3"
在Python中,不能在表达式中执行赋值。相反,Python解释这一点的方式是:

size = "Child 3"
etree.SubElement(root, "size").text = "Child 3"
您可以使用两行代码重写代码,以获得所需的结果:

size = etree.SubElement(root, "size")
size.text = "Child 3"

在浏览了
lxml
API之后,它似乎不是一种在一行中创建元素和为
text
属性赋值的方法。

谢谢!这解决了错误,我现在也明白了原因。请看我下面的回复谢谢!这解决了错误,我现在也明白了原因。请看下面我的回答