xml元素属性在python中列表

时间:2017-05-16 03:13:21

标签: python xml

我有一个类似xml的列表,我的goad是将每个counterPoint的X和Y放在一个列表中。你如何在python中实现它?提前谢谢。

<Assembly>
  <Part ASSEMBLY_POS="A12G3" PART_ID="2612666" PART_POS="Y366" MATERIAL="SN490B" PROFILE="PL12">
    <ContourPoint X="19700.001848" Y="21760.00407" Z="37863">
      <Chamfer DZ1="0" DZ2="0" TYPE="CHAMFER_NONE" X="0" Y="0" />
    </ContourPoint>
    <ContourPoint X="19700.001848" Y="21760.00407" Z="38213">
      <Chamfer DZ1="0" DZ2="0" TYPE="CHAMFER_NONE" X="0" Y="0" />
    </ContourPoint>
    <ContourPoint X="20018.001848" Y="21760.00407" Z="38213">
      <Chamfer DZ1="0" DZ2="0" TYPE="CHAMFER_ARC" X="35" Y="35" />
    </ContourPoint>
    <ContourPoint X="20018.001848" Y="21760.00407" Z="37507">
      <Chamfer DZ1="0" DZ2="0" TYPE="CHAMFER_ARC" X="35" Y="35" />
    </ContourPoint>
    <ContourPoint X="19820.001848" Y="21760.00407" Z="37507">
      <Chamfer DZ1="0" DZ2="0" TYPE="CHAMFER_NONE" X="0" Y="0" />
    </ContourPoint>
    <ContourPoint X="19820.001848" Y="21760.00407" Z="37863">
      <Chamfer DZ1="0" DZ2="0" TYPE="CHAMFER_ROUNDING" X="15" Y="0" />
    </ContourPoint>
  </Part>

</Assembly>

4 个答案:

答案 0 :(得分:0)

结帐https://docs.python.org/2/library/xml.etree.elementtree.html

import xml.etree.ElementTree as ET
tree = ET.parse('data.xml')
root = tree.getroot()
# root = ET.fromstring(data_str) # or read from a string

答案 1 :(得分:0)

第一个近似值可能是:

import xml.etree.ElementTree as ET

tree = ET.parse('file.xml')
root = tree.getroot()

# Or from a variable
# root = ET.fromstring(data_as_string)

part = root.getchildren()[0]

points = []
for child in part:
    points.append((float(child.attrib['X']), float(child.attrib['Y'])))

print(points)

来源:https://docs.python.org/3.5/library/xml.etree.elementtree.html

警告xml.etree.ElementTree模块对于恶意构造的数据不安全。如果您需要解析不受信任或未经身份验证的数据,请参阅XML漏洞。

答案 2 :(得分:0)

这是使用sax解析的替代方法,对于这种数据可能更简单。

data = """<?xml version="1.0"?>
<Assembly>
<Part ASSEMBLY_POS="A12G3" PART_ID="2612666" PART_POS="Y366" MATERIAL="SN490B" PROFILE="PL12">
    <ContourPoint X="19700.001848" Y="21760.00407" Z="37863">
    <Chamfer DZ1="0" DZ2="0" TYPE="CHAMFER_NONE" X="0" Y="0" />
    </ContourPoint>
    <ContourPoint X="19700.001848" Y="21760.00407" Z="38213">
    <Chamfer DZ1="0" DZ2="0" TYPE="CHAMFER_NONE" X="0" Y="0" />
    </ContourPoint>
    <ContourPoint X="20018.001848" Y="21760.00407" Z="38213">
    <Chamfer DZ1="0" DZ2="0" TYPE="CHAMFER_ARC" X="35" Y="35" />
    </ContourPoint>
    <ContourPoint X="20018.001848" Y="21760.00407" Z="37507">
    <Chamfer DZ1="0" DZ2="0" TYPE="CHAMFER_ARC" X="35" Y="35" />
    </ContourPoint>
    <ContourPoint X="19820.001848" Y="21760.00407" Z="37507">
    <Chamfer DZ1="0" DZ2="0" TYPE="CHAMFER_NONE" X="0" Y="0" />
    </ContourPoint>
    <ContourPoint X="19820.001848" Y="21760.00407" Z="37863">
    <Chamfer DZ1="0" DZ2="0" TYPE="CHAMFER_ROUNDING" X="15" Y="0" />
    </ContourPoint>
</Part>

</Assembly>
"""

import xml.sax

class AssemblyHandler(xml.sax.ContentHandler):
    def startElement(self, name, attrs):
        if name == "Part":
            print("PART")
        elif name == "ContourPoint":
            print(attrs.get('X'), attrs.get('Y'))

xml.sax.parseString(data, AssemblyHandler())

答案 3 :(得分:0)

查找具有相应标记名称的所有标记(在这种情况下具有属性“X”和“Y”)并迭代结果以使用列表推导将属性值获取到列表中!

data='''<Assembly>
  <Part ASSEMBLY_POS="A12G3" PART_ID="2612666" PART_POS="Y366" MATERIAL="SN490B" PROFILE="PL12">
    <ContourPoint X="19700.001848" Y="21760.00407" Z="37863">
      <Chamfer DZ1="0" DZ2="0" TYPE="CHAMFER_NONE" X="0" Y="0" />
    </ContourPoint>
    <ContourPoint X="19700.001848" Y="21760.00407" Z="38213">
      <Chamfer DZ1="0" DZ2="0" TYPE="CHAMFER_NONE" X="0" Y="0" />
    </ContourPoint>
    <ContourPoint X="20018.001848" Y="21760.00407" Z="38213">
      <Chamfer DZ1="0" DZ2="0" TYPE="CHAMFER_ARC" X="35" Y="35" />
    </ContourPoint>
    <ContourPoint X="20018.001848" Y="21760.00407" Z="37507">
      <Chamfer DZ1="0" DZ2="0" TYPE="CHAMFER_ARC" X="35" Y="35" />
    </ContourPoint>
    <ContourPoint X="19820.001848" Y="21760.00407" Z="37507">
      <Chamfer DZ1="0" DZ2="0" TYPE="CHAMFER_NONE" X="0" Y="0" />
    </ContourPoint>
    <ContourPoint X="19820.001848" Y="21760.00407" Z="37863">
      <Chamfer DZ1="0" DZ2="0" TYPE="CHAMFER_ROUNDING" X="15" Y="0" />
    </ContourPoint>
  </Part>
</Assembly>'''
import xml.etree.ElementTree as ET
root = ET.fromstring(data)
points = root.findall('.//ContourPoint')
print [(each.attrib['X'],each.attrib['Y']) for each in points]

输出:

[('19700.001848', '21760.00407'), ('19700.001848', '21760.00407'), ('20018.001848', '21760.00407'), ('20018.001848', '21760.00407'), ('19820.001848', '21760.00407'), ('19820.001848', '21760.00407')]