如何使用Python将xml文件属性附加为数组值? [英] How to append xml file attribute with array value using Python?
本文介绍了如何使用Python将xml文件属性附加为数组值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个xml文件.
<?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's approach seem easier, here is another way including the comments you need:
- 使用解析器读取文件以保留评论
- 使用
ET.Comment()
插入评论 - 循环浏览字典列表,并将子元素添加到xml
- 使用
toprettyxml()
将xml转换为格式化的字符串,但这会添加不必要的换行符 - 使用列表理解和
strip()
删除多余的换行符 - 将编码信息添加到xml声明
- 写入原始文件
- read your file using a parser to keep comments
- insert comments using
ET.Comment()
- loop through list of dictionaries and add sub-elements to xml
- use
toprettyxml()
to get convert xml to formatted string, but this adds unnecessary new-lines - remove the extra newlines using list comprehension and
strip()
- add encoding info to xml declaration
- 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 = '\n'.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=\"{}\"?>\n'.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文件属性附加为数组值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文