Python 如何编辑XML文件并保存在新文件中?

Python 如何编辑XML文件并保存在新文件中?,python,xml,python-3.x,Python,Xml,Python 3.x,我正在进行数据扩充。我想编辑一个XML文件并将其保存到一个新的XML文件中。以下是原始XML文件的示例: <object> <name>car</name> <pose>Unspecified</pose> <truncated>0</truncated> <difficult>0</difficult> <

我正在进行数据扩充。我想编辑一个XML文件并将其保存到一个新的XML文件中。以下是原始XML文件的示例:

   <object>
       <name>car</name>
       <pose>Unspecified</pose>
       <truncated>0</truncated>
       <difficult>0</difficult>
       <bndbox>
         <xmin>670</xmin>
         <ymin>108</ymin>
         <xmax>947</xmax>
         <ymax>265</ymax>
       </bndbox>
     </object>
     <object>
       <name>number_plate</name>
       <pose>Unspecified</pose>
       <truncated>0</truncated>
       <difficult>0</difficult>
       <bndbox>
         <xmin>808</xmin>
         <ymin>210</ymin>
         <xmax>865</xmax>
         <ymax>232</ymax>
       </bndbox>
     </object>

您从
中获取值并对其进行更改,但不会将其放回
-ie

xmin = bbox.find('xmin')
xmin.text = str(1280 - int(xmin.text))
并且您并没有使用文件的新名称来保存它


这是您的代码,但我没有运行它

import os
import xml.etree.ElementTree as ET

dirname = "/home/omarubuntu/PFEomar/depassement_stationnemen/conv-video_frame/Annotationflip_xml/"

for image_name in os.listdir(dirname):
    fullpath = os.path.join(dirname, image_name)

    tree = ET.parse(fullpath)

    objs = tree.findall('object')

    for ix, obj in enumerate(objs):
        bbox = obj.find('bndbox')

        xmin = bbox.find('xmin')
        xmin.text = str(1280 - int(xmin.text))

        xmax = bbox.find('xmax')
        xmax.text = str(1280 - int(xmax.text))

    new_fullpath = ospath.join(dirname, 'new_'+image_name)
    tree.write(new_fullpath)

这是我用来测试它的代码,它用新值显示
xml

import xml.etree.ElementTree as ET

text = '''<root>
     <object>
       <name>car</name>
       <pose>Unspecified</pose>
       <truncated>0</truncated>
       <difficult>0</difficult>
       <bndbox>
         <xmin>670</xmin>
         <ymin>108</ymin>
         <xmax>947</xmax>
         <ymax>265</ymax>
       </bndbox>
     </object>
     <object>
       <name>number_plate</name>
       <pose>Unspecified</pose>
       <truncated>0</truncated>
       <difficult>0</difficult>
       <bndbox>
         <xmin>808</xmin>
         <ymin>210</ymin>
         <xmax>865</xmax>
         <ymax>232</ymax>
       </bndbox>
     </object>
</root>'''


tree = ET.fromstring(text)

objs = tree.findall('object')

for ix, obj in enumerate(objs):
    bbox = obj.find('bndbox')

    xmin = bbox.find('xmin')
    xmin.text = str(1280 - int(xmin.text))

    xmax = bbox.find('xmax')
    xmax.text = str(1280 - int(xmax.text))

print(ET.tostring(tree).decode())
将xml.etree.ElementTree作为ET导入
文本='''
汽车
未指明
0
0
670
108
947
265
号码牌
未指明
0
0
808
210
865
232
'''
tree=ET.fromstring(文本)
objs=tree.findall('object')
对于ix,枚举中的obj(objs):
bbox=obj.find('bndbox')
xmin=bbox.find('xmin')
xmin.text=str(1280-int(xmin.text))
xmax=bbox.find('xmax')
xmax.text=str(1280-int(xmax.text))
打印(ET.tostring(tree.decode())

您从
中获取值并对其进行更改,但您不会将其放回
-ie

xmin = bbox.find('xmin')
xmin.text = str(1280 - int(xmin.text))
并且您并没有使用文件的新名称来保存它


这是您的代码,但我没有运行它

import os
import xml.etree.ElementTree as ET

dirname = "/home/omarubuntu/PFEomar/depassement_stationnemen/conv-video_frame/Annotationflip_xml/"

for image_name in os.listdir(dirname):
    fullpath = os.path.join(dirname, image_name)

    tree = ET.parse(fullpath)

    objs = tree.findall('object')

    for ix, obj in enumerate(objs):
        bbox = obj.find('bndbox')

        xmin = bbox.find('xmin')
        xmin.text = str(1280 - int(xmin.text))

        xmax = bbox.find('xmax')
        xmax.text = str(1280 - int(xmax.text))

    new_fullpath = ospath.join(dirname, 'new_'+image_name)
    tree.write(new_fullpath)

这是我用来测试它的代码,它用新值显示
xml

import xml.etree.ElementTree as ET

text = '''<root>
     <object>
       <name>car</name>
       <pose>Unspecified</pose>
       <truncated>0</truncated>
       <difficult>0</difficult>
       <bndbox>
         <xmin>670</xmin>
         <ymin>108</ymin>
         <xmax>947</xmax>
         <ymax>265</ymax>
       </bndbox>
     </object>
     <object>
       <name>number_plate</name>
       <pose>Unspecified</pose>
       <truncated>0</truncated>
       <difficult>0</difficult>
       <bndbox>
         <xmin>808</xmin>
         <ymin>210</ymin>
         <xmax>865</xmax>
         <ymax>232</ymax>
       </bndbox>
     </object>
</root>'''


tree = ET.fromstring(text)

objs = tree.findall('object')

for ix, obj in enumerate(objs):
    bbox = obj.find('bndbox')

    xmin = bbox.find('xmin')
    xmin.text = str(1280 - int(xmin.text))

    xmax = bbox.find('xmax')
    xmax.text = str(1280 - int(xmax.text))

print(ET.tostring(tree).decode())
将xml.etree.ElementTree作为ET导入
文本='''
汽车
未指明
0
0
670
108
947
265
号码牌
未指明
0
0
808
210
865
232
'''
tree=ET.fromstring(文本)
objs=tree.findall('object')
对于ix,枚举中的obj(objs):
bbox=obj.find('bndbox')
xmin=bbox.find('xmin')
xmin.text=str(1280-int(xmin.text))
xmax=bbox.find('xmax')
xmax.text=str(1280-int(xmax.text))
打印(ET.tostring(tree.decode())

如果只需要向xmin/xmax元素添加100个元素,那么可以使用xslt标识转换模式,让libxml来完成繁重的工作:

from lxml import etree

xsl = etree.XML('''
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <xsl:apply-templates/>
    </xsl:template>

    <!-- match xmin and xmax elements -->
    <xsl:template match="xmin|xmax">
        <xsl:copy>
            <!-- add 100 to the value of the current node -->
            <xsl:value-of select=". + 100" />
        </xsl:copy>
    </xsl:template>

    <!-- recursively copy the rest of the xml document -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>
''')

transform = etree.XSLT(xsl)
with open("original.xml") as f:
    print(transform(etree.parse(f)), end='')
如果样式表存储在文件augmentation.xsl中,则可以使用libxml实用程序xsltproc获得相同的结果:

diff original.xml <(xsltproc augmentation.xsl original.xml)

diff original.xml如果只需要向xmin/xmax元素添加100个元素,那么可以使用xslt标识转换模式,让libxml完成繁重的工作:

from lxml import etree

xsl = etree.XML('''
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <xsl:apply-templates/>
    </xsl:template>

    <!-- match xmin and xmax elements -->
    <xsl:template match="xmin|xmax">
        <xsl:copy>
            <!-- add 100 to the value of the current node -->
            <xsl:value-of select=". + 100" />
        </xsl:copy>
    </xsl:template>

    <!-- recursively copy the rest of the xml document -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>
''')

transform = etree.XSLT(xsl)
with open("original.xml") as f:
    print(transform(etree.parse(f)), end='')
如果样式表存储在文件augmentation.xsl中,则可以使用libxml实用程序xsltproc获得相同的结果:

diff original.xml <(xsltproc augmentation.xsl original.xml)

diff original.xml您从
中获取值,但您不会对其进行更改,也不会将其放回
,所以您希望得到什么。您的缩进错误,所以应该得到错误。您从
中得到值,但您不会更改它,也不会将它放回
,所以您希望得到什么。你们有错误的缩进,所以你们应该得到错误。