• <bdo id='7TvMw'></bdo><ul id='7TvMw'></ul>

    1. <legend id='7TvMw'><style id='7TvMw'><dir id='7TvMw'><q id='7TvMw'></q></dir></style></legend>

      <small id='7TvMw'></small><noframes id='7TvMw'>

        <i id='7TvMw'><tr id='7TvMw'><dt id='7TvMw'><q id='7TvMw'><span id='7TvMw'><b id='7TvMw'><form id='7TvMw'><ins id='7TvMw'></ins><ul id='7TvMw'></ul><sub id='7TvMw'></sub></form><legend id='7TvMw'></legend><bdo id='7TvMw'><pre id='7TvMw'><center id='7TvMw'></center></pre></bdo></b><th id='7TvMw'></th></span></q></dt></tr></i><div id='7TvMw'><tfoot id='7TvMw'></tfoot><dl id='7TvMw'><fieldset id='7TvMw'></fieldset></dl></div>

      1. <tfoot id='7TvMw'></tfoot>

        如何在解析之前检查 XML 中是否存在属性和标签?

        时间:2023-08-29
        <i id='GV9NG'><tr id='GV9NG'><dt id='GV9NG'><q id='GV9NG'><span id='GV9NG'><b id='GV9NG'><form id='GV9NG'><ins id='GV9NG'></ins><ul id='GV9NG'></ul><sub id='GV9NG'></sub></form><legend id='GV9NG'></legend><bdo id='GV9NG'><pre id='GV9NG'><center id='GV9NG'></center></pre></bdo></b><th id='GV9NG'></th></span></q></dt></tr></i><div id='GV9NG'><tfoot id='GV9NG'></tfoot><dl id='GV9NG'><fieldset id='GV9NG'></fieldset></dl></div>

        <small id='GV9NG'></small><noframes id='GV9NG'>

          <tfoot id='GV9NG'></tfoot>
              <legend id='GV9NG'><style id='GV9NG'><dir id='GV9NG'><q id='GV9NG'></q></dir></style></legend>
                <bdo id='GV9NG'></bdo><ul id='GV9NG'></ul>

                    <tbody id='GV9NG'></tbody>
                  本文介绍了如何在解析之前检查 XML 中是否存在属性和标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在通过 Python 中的元素树解析 XML 文件,并将内容写入 cpp 文件.

                  I'm parsing an XML file via Element Tree in python and and writing the content to a cpp file.

                  子标签的内容会因不同的标签而异.例如,第一个事件标签将派对标签作为子标签,但第二个事件标签没有.

                  The content of children tags will be variant for different tags. For example first event tag has party tag as child but second event tag doesn't have.

                  -->如何在解析前检查标签是否存在?

                  -->How can I check whether a tag exists or not before parsing?

                  -->Children 在第一个事件标签中具有 value 属性,但在第二个事件标签中没有.如何在取值之前检查属性是否存在.

                  -->Children has value attribute in 1st event tag but not in second. How can I check whether an attribute exists or not before taking it's value.

                  --> 目前我的代码对不存在的派对标签抛出错误,并为第二个子标签设置无"属性值.

                  --> Currently my code throws an error for non existing party tag and sets a "None" attribute value for the second children tag.

                  <main>
                    <event>
                      <party>Big</party>
                      <children type="me" value="3"/>
                    </event>
                  
                    <event>
                      <children type="me"/>
                    </event>
                  
                  </main>
                  

                  代码:

                  import xml.etree.ElementTree as ET
                  tree = ET.parse('party.xml')
                  root = tree.getroot()
                  for event in root.findall('event'):
                      parties = event.find('party').text
                      children = event.get('value')
                  

                  我想检查标签,然后取它们的值.

                  I want to check the tags and then take their values.

                  推荐答案

                  如果标签不存在,.find() 确实返回 None.只需测试该值:

                  If a tag doesn't exist, .find() indeed returns None. Simply test for that value:

                  for event in root.findall('event'):
                      party = event.find('party')
                      if party is None:
                          continue
                      parties = party.text
                      children = event.get('value')
                  

                  您已经在事件上使用 .get() 来测试 value 属性;如果属性不存在,它也会返回 None.

                  You already use .get() on event to test for the value the attribute; it returns None as well if the attribute does not exist.

                  属性存储在 .attrib 字典中,因此您也可以使用标准 Python 技术来显式测试属性:

                  Attributes are stored in the .attrib dictionary, so you can use standard Python techniques to test for the attribute explicitly too:

                  if 'value' in event.attrib:
                      # value attribute is present.
                  

                  这篇关于如何在解析之前检查 XML 中是否存在属性和标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:何时在 python 中使用 if vs elif 下一篇:Pandas:如何根据其他列值的条件对列进行求和?

                  相关文章

                1. <small id='MExlQ'></small><noframes id='MExlQ'>

                  <i id='MExlQ'><tr id='MExlQ'><dt id='MExlQ'><q id='MExlQ'><span id='MExlQ'><b id='MExlQ'><form id='MExlQ'><ins id='MExlQ'></ins><ul id='MExlQ'></ul><sub id='MExlQ'></sub></form><legend id='MExlQ'></legend><bdo id='MExlQ'><pre id='MExlQ'><center id='MExlQ'></center></pre></bdo></b><th id='MExlQ'></th></span></q></dt></tr></i><div id='MExlQ'><tfoot id='MExlQ'></tfoot><dl id='MExlQ'><fieldset id='MExlQ'></fieldset></dl></div>
                  <legend id='MExlQ'><style id='MExlQ'><dir id='MExlQ'><q id='MExlQ'></q></dir></style></legend>

                    1. <tfoot id='MExlQ'></tfoot>
                      • <bdo id='MExlQ'></bdo><ul id='MExlQ'></ul>