如何从<reg regname=“uid\u v2”type=“block\u uid\u v2\u t”num=“1”addr=“0x4”incr=“0x4”>

2024-09-27 23:27:45 发布

您现在位置:Python中文网/ 问答频道 /正文

  1. 列表项

[在此处输入图像描述][1]。你知道吗

如何分别获取regname,type。。。在python中。我需要得到类型值和地址分别。你知道吗

    for regfile in root.findall('regfile'):
        reg_value = regfile.find('reg').text
        print(reg_value)
        for reg in regfile.findall('reg'):
            name = reg.find('name').text
            type = reg.find('text').text

我试过了,但它抛出了类似“AttributeError:'NoneType'对象没有属性'text'”的错误

    <regfile regfilename="blkreq" type="blk_required_v2_rdl_rf_t" num="1" addr="0x0" incr="0x8">
        <name><![CDATA[blk_required_v2_rdl_rf_t]]></name>
        <description><![CDATA[]]></description>
        <property name="num" value="1"/>
        <reg regname="uid_v2" type="block_uid_v2_t" num="1" addr="0x4" incr="0x4">
            <name><![CDATA[Block Unique ID Type]]></name>
            <description><![CDATA[The IPID and the Platform Type together provides Unique ID of an IP]]></description>
            <property name="msbhigh" value="1"/>
            <property name="num" value="1"/>
            <property name="retention" value=""/>
            <property name="width" value="32"/>
            <property name="shared" value="0"/>
            <property name="accesswidth" value="8"/>
            <field fieldname="ipid">
                <name><![CDATA[Design IP Identification]]></name>
                <description><![CDATA[This is reserved for future use.]]></description>
                <msb>31</msb>
                <lsb>8</lsb>
                <property name="hw_r" value="1"/>
                <property name="reset" value="0h"/>
                <property name="retention" value=""/>
                <property name="sw_r" value="1"/>
                <property name="width" value="0"/>
            </field>
            <field fieldname="platform">
                <name><![CDATA[Platform]]></name>
                <description><![CDATA[Implementation for the specific platform class (asic, fpga, vp, etc) - supplied 
           a global parameter, PLATFORM_TYPE, by the RTL and defined in the platform package in DVI_LIB. 
           The reset value shall reflect the configuration.]]></description>
                <msb>3</msb>
                <lsb>0</lsb>
                <property name="hw_w" value="1"/>
                <property name="reset" value=""/>
                <property name="retention" value=""/>
                <property name="sw_r" value="1"/>
                <property name="width" value="0"/>
            </field>
        </reg>

Tags: thetextnameinfieldforvaluetype
2条回答

您可以使用xml.etree.ElementTree包将xml代码解析到树中,并按以下方式对其进行迭代:

# Official DOC: 
# https://docs.python.org/3/library/xml.etree.elementtree.html#module-xml.etree.ElementTree

import xml.etree.ElementTree as ET

x = '''<?xml version="1.0"?>
<regs>
    <reg regname="uid_v2" type="block_uid_v2_t" num="1" addr="0x4" incr="0x4"> </reg>
    <reg regname="uid_v3" type="block_uid_v3_t" num="3" addr="0x4" incr="0x4"> </reg>
</regs>'''

# tree = ET.parse("some_file.xml")
tree = ET.fromstring(x) # I use a string here in this example

for child in tree:
    print(child.tag, child.attrib)

child.attrib是一个字典:您可以通过child.attrib["type"]简单地获得特定的属性类型。看看这个复制对于live example。你知道吗

编辑-根据以下评论中的要求:

# Get only the relevant nodes from the tree
for child in tree:
    if "type" in child.attrib:
        if child.attrib["type"] == "block_uid_v2_t":
            print(child.tag, child.attrib)

# List comprehension to store all relevant nodes in a list
regs = [child.attrib for child in tree if ("type" in child.attrib and child.attrib["type"]=="block_uid_v2_t")]

# Store only 'regname' of relevant nodes
reg_ids = [child.attrib["regname"] for child in tree if ("type" in child.attrib and child.attrib["type"]=="block_uid_v2_t")]
    for regfile in root.findall('regfile'):
        for reg in regfile.findall('reg'):
            reg_name = reg.find('name').text
            reg_name_quotes = '"'+reg_name+'"'
            reg_type = reg.get('type')
            reg_addr = reg.get('addr')
            reg_description = reg.find('description').text

使用get方法可以得到属性。你知道吗

相关问题 更多 >

    热门问题