修改xml标签

参考:https://blog.csdn.net/LOVE1055259415/article/details/79166754

import  xml.dom.minidom

dom=xml.dom.minidom.parse('/media/d_2/everyday/0727/xml/mingpai_0725_2.xml')
root=dom.documentElement
name=root.getElementsByTagName('name')

for i in range(len(name)):
    if 'CJHao' == name[i].firstChild.data:
        print "find___88"
        name[i].firstChild.data = 'chejiahao'
        
with open("/media/d_2/everyday/0727/xml/change.xml",'w') as fh:
    dom.writexml(fh)
    print('写入name/pose OK!')

晕死,还必须另外保存一个文件才能看到更改后的,不是就地修改的

<?xml version="1.0" ?><annotation>
   <folder>VOC2007</folder>
   <filename>mingpai_0725_2.jpg</filename>
   <source>
       <database>The VOC2007 Database</database>
       <annotation>PASCAL VOC2007</annotation>
       <image>flickr</image>
       <flickrid>329145082</flickrid>
   </source>
   <owner>&gt;
       <flickrid>hiromori2</flickrid>
       <name>Hiroyuki Mori</name>
   </owner>&gt;
   <size>
       <width>1227</width>
       <height>401</height>
       <depth>3</depth>
   </size>
   <segmented>0</segmented>
   <object>
       <name>ZhiZaoNianYue</name>
       <pose>Unspecified</pose>
       <truncated>0</truncated>
       <difficult>0</difficult>
       <bndbox>
           <xmin>871</xmin>
           <ymin>278</ymin>
           <xmax>1055</xmax>
           <ymax>316</ymax>
       </bndbox>
   </object>
   <object>
       <name>FDJ_PaiL</name>
       <pose>Unspecified</pose>
       <truncated>0</truncated>
       <difficult>0</difficult>
       <bndbox>
           <xmin>434</xmin>
           <ymin>353</ymin>
           <xmax>604</xmax>
           <ymax>389</ymax>
       </bndbox>
   </object>
   <object>
       <name>FDJ_Hao</name>
       <pose>Unspecified</pose>
       <truncated>0</truncated>
       <difficult>0</difficult>
       <bndbox>
           <xmin>111</xmin>
           <ymin>356</ymin>
           <xmax>261</xmax>
           <ymax>389</ymax>
       </bndbox>
   </object>
   <object>
       <name>CJHao</name>
       <pose>Unspecified</pose>
       <truncated>0</truncated>
       <difficult>0</difficult>
       <bndbox>
           <xmin>344</xmin>
           <ymin>161</ymin>
           <xmax>928</xmax>
           <ymax>228</ymax>
       </bndbox>
   </object>
</annotation>

猜你喜欢

转载自blog.csdn.net/yang332233/article/details/81238985