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

        <bdo id='oAAY0'></bdo><ul id='oAAY0'></ul>
    1. <small id='oAAY0'></small><noframes id='oAAY0'>

      如何使用 Python 将 xml 文件属性附加到数组值?

      时间:2023-09-27

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

              • <tfoot id='jB4sx'></tfoot>

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

                  <tbody id='jB4sx'></tbody>

                本文介绍了如何使用 Python 将 xml 文件属性附加到数组值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个 xml 文件.

                I have an xml file.

                <?xml version='1.0' encoding='utf-8'?>
                <systemdata>
                     <process>
                          <number code="hsfg" class="hgdgf" tool="gagfa">
                               <value type="string" />
                               <value type="None" />
                          </number>
                          <!-- ID -->
                          <id code="hsfg" class="gfdg" tool="fadg">
                               <value type="string" />
                               <value type="None" />
                          </id>
                     </process>
                </systemdata>
                

                我想将此数组附加到上面的 XML 文件中.

                I would like to append this array to my XML file above.

                memorys = []
                for mem in wmiquery.Win32_PhysicalMemory():
                    sysmem = {}
                    sysmem['location'] = mem.DeviceLocator
                    sysmem['banklabel'] = mem.BankLabel
                    sysmem['cap'] = mem.Capacity
                    memorys.append(sysmem)
                for m in memorys:
                    print(m)
                

                m的值是这样的:

                {'location': 'DIMM1', 'banklabel': 'ChannelA', 'cap': '8589934592'}
                {'location': 'DIMM2', 'banklabel': 'ChannelA', 'cap': '8589934592'}
                

                我想将这些数组附加到我的 XML 中.所以我的期望基于上面的数组,我将追加 2 个新元素.如果数组有 4 则创建新的 4 元素.这是我的期望输出:

                I would like to append these array to my XML. So my expectation based on the array above, I will append 2 new element. If the array has 4 then create new 4 element. Here is my expectation output:

                <?xml version='1.0' encoding='utf-8'?>
                <systemdata>
                     <process>
                          <number code="hsfg" class="hgdgf" tool="gagfa">
                               <value type="string" />
                               <value type="None" />
                          </number>
                          <!-- ID -->
                          <id code="hsfg" class="gfdg" tool="fadg">
                               <value type="string" />
                               <value type="None" />
                          </id>
                     </process>
                     <!-- memory -->
                     <unitmemory>
                          <!-- data -->
                          <module location="DIMM1">
                               <banklabel tool="banklabel">
                                    <value type="string">ChannelA</value>
                               </banklabel>
                               <cap tool="cap">
                                    <value type="string">8589934592</value>
                               </cap>
                          </module>             
                          <module location="DIMM2">
                               <banklabel tool="banklabel">
                                    <value type="string">ChannelA</value>
                               </banklabel>
                               <cap tool="cap">
                                    <value type="string">8589934592</value>
                               </cap>
                          </module>
                     </unitmemory>
                </systemdata>
                

                任何人都可以给我一个想法和帮助,拜托.我真的很感激.

                Anyone can give me an idea and help, please. I really appreciated and thankful.

                推荐答案

                Jack 的方法似乎更简单,这里有另一种方法,包括您需要的注释:

                Jack's approach seem easier, here is another way including the comments you need:

                1. 使用解析器读取您的文件以保留评论
                2. 使用 ET.Comment()
                3. 插入评论
                4. 遍历字典列表并将子元素添加到 xml
                5. 使用 toprettyxml() 将 xml 转换为格式化字符串,但这会添加不必要的换行符
                6. 使用列表理解和 strip()
                7. 删除多余的换行符
                8. 在 xml 声明中添加编码信息
                9. 写入原始文件
                1. read your file using a parser to keep comments
                2. insert comments using ET.Comment()
                3. loop through list of dictionaries and add sub-elements to xml
                4. use toprettyxml() to get convert xml to formatted string, but this adds unnecessary new-lines
                5. remove the extra newlines using list comprehension and strip()
                6. add encoding info to xml declaration
                7. write to original file

                import xml.etree.ElementTree as ET
                import xml.dom.minidom
                
                memorys = [
                    {'location': 'DIMM1', 'banklabel': 'ChannelA', 'cap': '100'},
                    {'location': 'DIMM2', 'banklabel': 'ChannelB', 'cap': '200'}
                ]
                
                m_encoding = 'utf-8'
                parser = ET.XMLParser(target=ET.TreeBuilder(insert_comments=True))
                
                tree = ET.parse('sampleXml.xml', parser=parser)
                root = tree.getroot()
                
                root.insert(1, ET.Comment('memory'))
                
                unit_mem = ET.SubElement(root, 'unitmemory')
                unit_mem.insert(0, ET.Comment('data'))
                
                for mem in memorys:
                    m_module = ET.SubElement(unit_mem, 'module ')
                    m_module.set('location', mem['location'])
                
                    b_label = ET.SubElement(m_module, 'banklabel  ')
                    m_cap = ET.SubElement(m_module, 'cap ')
                    b_value = ET.SubElement(b_label, 'value ')
                    c_value = ET.SubElement(m_cap, 'value ')
                
                    m_module.set('location', mem['location'])
                    b_label.set('tool', 'banklabel')
                    m_cap.set('tool', 'cap')
                    b_value.set('type', 'string')
                    c_value.set('type', 'string')
                
                    b_value.text = mem['banklabel']
                    c_value.text = mem['cap']
                
                dom = xml.dom.minidom.parseString(ET.tostring(root))
                xml_string = dom.toprettyxml()
                xml_string = '
                '.join([line for line in xml_string.splitlines() if line.strip()])
                part1, part2 = xml_string.split('?>')
                
                with open("sampleXml.xml", 'w') as xfile:
                    xfile.write(part1 + 'encoding="{}"?>
                '.format(m_encoding) + part2)
                    xfile.close()
                

                我的意见

                {'location': 'DIMM1', 'banklabel': 'ChannelA', 'cap': '100'}
                {'location': 'DIMM2', 'banklabel': 'ChannelB', 'cap': '200'}
                

                我的输出

                <?xml version="1.0" encoding="utf-8"?>
                
                <systemdata>
                    <process>
                        <number code="hsfg" class="hgdgf" tool="gagfa">
                            <value type="string"/>
                            <value type="None"/>
                        </number>
                        <!-- ID -->
                        <id code="hsfg" class="gfdg" tool="fadg">
                            <value type="string"/>
                            <value type="None"/>
                        </id>
                    </process>
                    <!--memory-->
                    <unitmemory>
                        <!--data-->
                        <module location="DIMM1">
                            <banklabel tool="banklabel">
                                <value type="string">ChannelA</value>
                            </banklabel>
                            <cap tool="cap">
                                <value type="string">100</value>
                            </cap>
                        </module>
                        <module location="DIMM2">
                            <banklabel tool="banklabel">
                                <value type="string">ChannelB</value>
                            </banklabel>
                            <cap tool="cap">
                                <value type="string">200</value>
                            </cap>
                        </module>
                    </unitmemory>
                </systemdata>
                

                这篇关于如何使用 Python 将 xml 文件属性附加到数组值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:ndarray 比 recarray 访问快吗? 下一篇:附加两个具有相同列、不同顺序的数据框

                相关文章

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

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