将编辑过的xml内容写入另一个文件

2024-05-20 17:22:09 发布

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

我有两个xml文件,如下所示,我想检查文件B和文件A的顺序(文件B应该遵循文件A的顺序)。我还在下面写了一个程序来维护顺序,唯一的问题是我不能正确地将输出写入另一个xml文件。在问这个问题之前,我确实研究过如何将编辑过的xml文件写回源文件或其他文件,但可能我遗漏了一些非常小的东西。你知道吗

文件A

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<p1:sample1 xmlns:p1="http://www.example.org/eHorizon">
<p1:time nTimestamp="1">
   <p1:location hours = "1" path = '1'>       
      <p1:feature color="6" type="a">560</p1:feature>
   </p1:location>
</p1:time>
<p1:time nTimestamp="2">
   <p1:location hours = "1" path = '1'>
      <p1:feature color="2" type="a">564</p1:feature>         
   </p1:location>
</p1:time>
<p1:time nTimestamp="3">
   <p1:location hours = "1" path = '1'>       
      <p1:feature color="6" type="a">560</p1:feature>          
   </p1:location>
</p1:time>
</p1:sample1>

文件B

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<p1:sample1 xmlns:p1="http://www.example.org/eHorizon">
<p1:time nTimestamp="1">
   <p1:location hours = "1" path = '1'>       
      <p1:feature color="6" type="a">560</p1:feature>     
   </p1:location>
</p1:time>
<p1:time nTimestamp="3">
   <p1:location hours = "1" path = '1'>
      <p1:feature color="6" type="a">560</p1:feature>     
   </p1:location>
</p1:time>
<p1:time nTimestamp="2">
   <p1:location hours = "1" path = '1'>       
      <p1:feature color="2" type="a">564</p1:feature>      
   </p1:location>
</p1:time>
</p1:sample1>

仅供参考,这里唯一的区别是用nTimestamps表示的整个p1:time元素及其子元素(如locationfeature)的顺序。您可以看到在文件A中它是1,2,3...,而在文件B中它是1,3,2...(我指的是整个p1:time元素和其中的所有内容)

我想要什么

from lxml import etree
from collections import defaultdict
from distutils.filelist import findall
from lxml._elementpath import findtext



recovering_parser = etree.XMLParser(recover=True)

Reference = etree.parse("C:/Users/your_location/Desktop/sample1.xml", parser=recovering_parser)
Copy = etree.parse("C:/Users/your_location/Desktop/sample2.xml", parser=recovering_parser)


ReferenceTest = Reference.findall("{http://www.example.org/eHorizon}time") #find all time elements in sample1
CopyTest = Copy.findall("{http://www.example.org/eHorizon}time") #find all time elements in sample2

a=[] #list for storing sample1's Time elements
b=[] #list for storing sample2's Time elements
new_list=[] #for storing sorted data

for i,j in zip(ReferenceTest,CopyTest):

    a.append((i, i.attrib.get("nTimestamp"))) # store data in format [(<Element {http://www.example.org/eHorizon}time at 0x213d738>, '1')  
                                              # where 1,2 or 3 is ntimestamp attribute and corresponding parent 'time' element of that attribute
    b.append((j, j.attrib.get("nTimestamp"))) # same as above 

def sortTimestamps(a,b):   #use this function to sort elements in 'b' list in such a manner that they follow sequence of 'a' list 

    for i in a:
        for j in b:
            if i[1]==j[1]:
                s = a.index(i)
                t = b.index(j)
                b[t],b[s]=b[s],b[t]     



sortTimestamps(a, b)  # call sort function 

for i in b:
    new_list.append(i[0]) # store the sorted timestamps in new_list


CopyTest = new_list # assign new sorted list of time elements to old list

Copy.write("C:/Users/your_location/Desktop/output_data.xml") # write data to another file and check results 

上面的代码按照文件A的顺序对B文件进行排序,但是当我将程序写入另一个文件时,它会按原样写入文件B的数据。也就是说,它以与上面文件B中所示相同的方式写入数据。在排序之后,我希望文件B的数据顺序应该被修改,它应该按照文件A

我尝试的

除了上面的程序外,我还试着读更多关于文件写作的内容,但这让我一事无成。我检查了我的xml格式,我相信这是完全正确的。最后,我还学习了教程here,只是想看看它是如何显示写作的,但是这种方法也不起作用。也许你们能帮我。你知道吗

编辑:我从链接中删除了代码并添加到这里。我以前这么做是为了防止长时间的发帖


Tags: 文件pathinfortime顺序locationxml
1条回答
网友
1楼 · 发布于 2024-05-20 17:22:09

这不起作用,您只是将一个新列表分配给旧列表-CopyTest。这不会改变实际xml中的任何内容。你知道吗

最简单的方法是从new_list中的元素再次创建xml。示例-

root = etree.Element('{http://www.example.org/eHorizon}sample1',nsmap={'p1':'http://www.example.org/eHorizon'})
for elem in new_list:
    root.append(elem)

etree.ElementTree(root).write("c.xml") # write data to another file and check results 

您应该替换上面的行,替换CopyTest = new_list和后面的行。你知道吗

相关问题 更多 >