我正在编写ElementTree python代码,该代码将:
问题是XML被一遍又一遍覆盖,我想要的是将数据附加到新xml文件的末尾。
到目前为止,这是我的代码。
base_path = os.path.dirname(os.path.realpath(__file__))
xml_file = os.path.join(base_path, 'data.xml')
tree = ET.parse(xml_file)
root = tree.getroot()
with open('a.txt', 'r') as f:
d = f.readlines()
itemIDlist = []
for i in range(len(d)-1):
hold = d[i].split('\n')
itemIDlist.append(str(hold[0]))
for e in root.iter():
if 'itemID' in e.keys():
e.set('itemID', itemIDlist[i])
new_data = ET.tostringlist(root)
new_xml = open('newXml.xml', 'w')
new_xml.write(new_data)
文本文件中有一个数字列表,例如:我们称之为a.txt
19283
29354
34958
59854
原始XML文件如下所示
<requests>
<inventory name="WaterMelon" itemID="6834833" action="inStock">
<user username="Carlos">
<role name="GeneralManager"/>
</user>
</inventory>
<inventory name="StrawBerry" itemID="9283343" action="inStock">
<user username="Carlos">
<role name="GeneralManager"/>
</user>
</inventory>
<inventory name="WaterMelon" itemID="" action="inStock">
<user username="Carlos">
<role name="GeneralManager"/>
</user>
</inventory>-->
</requests>
当前,我得到四个不同的文件,但是,我只想获得一个包含所有结果的XML文件。看起来应该像这样
<requests>
<inventory action="inStock" itemID="19283" name="WaterMelon">
<user username="Carlos">
<role name="GeneralManager" />
</user>
</inventory>
<inventory action="inStock" itemID="19283" name="StrawBerry">
<user username="Carlos">
<role name="GeneralManager" />
</user>
</inventory>
<inventory action="inStock" itemID="19283" name="WaterMelon">
<user username="Carlos">
<role name="GeneralManager" />
</user>
</inventory>-->
<inventory action="inStock" itemID="29354" name="WaterMelon">
<user username="Carlos">
<role name="GeneralManager" />
</user>
</inventory>
<inventory action="inStock" itemID="29354" name="StrawBerry">
<user username="Carlos">
<role name="GeneralManager" />
</user>
</inventory>
<inventory action="inStock" itemID="29354" name="WaterMelon">
<user username="Carlos">
<role name="GeneralManager" />
</user>
</inventory>-->
<inventory action="inStock" itemID="34958" name="WaterMelon">
<user username="Carlos">
<role name="GeneralManager" />
</user>
</inventory>
<inventory action="inStock" itemID="34958" name="StrawBerry">
<user username="Carlos">
<role name="GeneralManager" />
</user>
</inventory>
<inventory action="inStock" itemID="34958" name="WaterMelon">
<user username="Carlos">
<role name="GeneralManager" />
</user>
</inventory>-->
</requests>
请帮助
答案 0 :(得分:0)
此链接有相同的问题。 How to write xml file with multiple root element using ElementTree in python
我将字符串串联起来。
pct_df.plot(kind="bar", stacked=True)
这给了我们预期的结果;一个xml文件。
@see <a href="http://google.com">http://google.com</a>
仍然存在问题,当生成xml时,会有一个>不能正确解释并输出的信息
with open('a.txt', 'r') as f:
d = f.readlines()
itemIDlist = []
new_data = ''
for i in range(len(d)-1):
hold = d[i].split('\n')
itemIDlist.append(str(hold[0]))
for e in root.iter():
if 'itemID' in e.keys():
e.set('itemID', itemIDlist[i])
new_data += ET.tostring(root)
new_xml = open('newXml.xml', 'w')
new_xml.write(new_data)
代替 <requests>
<inventory action="inStock" itemID="19283" name="WaterMelon">
<user username="Carlos">
<role name="GeneralManager" />
</user>
</inventory>
<inventory action="inStock" itemID="19283" name="StrawBerry">
<user username="Carlos">
<role name="GeneralManager" />
</user>
</inventory>
<inventory action="inStock" itemID="19283" name="WaterMelon">
<user username="Carlos">
<role name="GeneralManager" />
</user>
</inventory>-->
</requests><requests>
<inventory action="inStock" itemID="29354" name="WaterMelon">
<user username="Carlos">
<role name="GeneralManager" />
</user>
</inventory>
<inventory action="inStock" itemID="29354" name="StrawBerry">
<user username="Carlos">
<role name="GeneralManager" />
</user>
</inventory>
<inventory action="inStock" itemID="29354" name="WaterMelon">
<user username="Carlos">
<role name="GeneralManager" />
</user>
</inventory>-->
</requests><requests>
<inventory action="inStock" itemID="34958" name="WaterMelon">
<user username="Carlos">
<role name="GeneralManager" />
</user>
</inventory>
<inventory action="inStock" itemID="34958" name="StrawBerry">
<user username="Carlos">
<role name="GeneralManager" />
</user>
</inventory>
<inventory action="inStock" itemID="34958" name="WaterMelon">
<user username="Carlos">
<role name="GeneralManager" />
</user>
</inventory>-->
</requests>