python query

时间:2016-08-01 07:13:01

标签: python xml

我正在尝试在python中复制一些XML代码以放入我正在处理的程序中,该程序执行一些全景捕获。最后的想法是导出捕获细节的XML文件,以便更容易地导入各种全景捕获程序之一。

我对Python很新,但是一直在使用xml.etree.ElementTree,我可以设置像根声明和标题和子标题这样的信息,但是我在两点中有点丢失,第一个是如何通过子元素我可以设置一个值(例如GPS),第二个是子元素如何具有多个值(例如马赛克/重叠最小值)。

对于我在下面工作的元素;

root = etree.Element("papywizard")
root.set("version", "c")
header = etree.SubElement(root,"header")
general = etree.SubElement(header, "general")
title = etree.SubElement(general,"title")

然后我想我可以做title.text("Test123")这样的事情,但这不起作用。我试图复制的完整XML是下面的,有人能够指出我如何在子元素标签中设置文本的正确方向,并且可以将多少标签聚合到一个子元素中?< / p>

非常感谢!

<?xml version="1.0" encoding="utf-8"?>
<papywizard version="c">
    <header>
        <general>
            <title>
                Test Capture 1
            </title>
            <gps>
                37.8022697,-122.4056749
            </gps>
            <comment>
                Add your comments here
            </comment>
        </general>
        <shooting mode="mosaic">
            <headOrientation>
                up
            </headOrientation>
            <cameraOrientation>
                landscape
            </cameraOrientation>
            <stabilizationDelay>
               5.0
            </stabilizationDelay>
            <counter>
                001
            </counter>
            <startTime>
               2014-02-23_13h59m01s
            </startTime>
            <endTime>
               2014-02-23_13h53m33s
            </endTime>
       </shooting>
       <camera>
            <timeValue>
               5.0
            </timeValue>
            <bracketing nbPicts="1"/>
            <sensor coef ="4.74" ratio="4:3"/>
       </camera>
       <lens type="rectilinear">
            <focal>
               12.7
            </focal>
       </lens>
       <mosaic>
            <nbPicts pitch="5" yaw="10"/>
            <overlap minimum="0.25" pitch="0.25" yaw="0.25"/>
       </mosaic>
    </header>
    <shoot>
        <pict bracket="1" id="1">
            <time>
                2014-02-23_13h59m01s
            </time>
            <position pitch="37.96" roll="0.0"  yaw="-99.96"/>
        </pict>
        <pict bracket="1" id="2">
            <time>
                2014-02-23_13h59m01s
            </time>
            <position pitch="18.98" roll="0.0"  yaw="-99.96"/>
        </pict>
        <pict bracket="1" id="3">
            <time>
                2014-02-23_13h59m01s
            </time>
            <position pitch="0.00" roll="0.0"  yaw="-99.96"/>
        </pict>
    </shoot>
</papywizard>

3 个答案:

答案 0 :(得分:2)

import xml.etree.ElementTree as ET

root = ET.Element("papywizard")
root.set("version", "c")
header = ET.SubElement(root,"header")
general = ET.SubElement(header, "general")
title = ET.SubElement(general,"title")
title.text = str('Test123') # This is how you set it

tree = ET.ElementTree(root) # This step will form a tree
tree.write('expected.xml') # This step will save the xml file.

答案 1 :(得分:1)

您必须使用以下命令:

title.text = "some text"

答案 2 :(得分:1)

文本节点和元素节点是两种节点,XML元素节点可以按任意顺序包含任意数量的文本和/或元素子节点。

如果要向节点添加文本,可以使用.text属性

执行此操作
title.text = "Sometext"

如果要添加属性,可以使用set命令

执行此操作
title.set('Attribute name', 'Attributevalue')