In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article analyzes "how python modifies the information in xml files in batches". The content is detailed and easy to understand, and friends who are interested in "how python batch modifies the information in xml files" can follow the editor's train of thought to read it in depth. I hope it will be helpful to everyone after reading. Let's follow the editor to learn more about "how python modifies the information in xml files in batches".
What does python mean Python is a cross-platform, interpretive, compiled, interactive and object-oriented scripting language, originally designed to write automated scripts, and is often used to develop independent and large-scale projects as versions are constantly updated and new features are added.
Project scenario:
When doing target detection, re-tagging will take a lot of time. If you can modify the information in xml in batches, it will save a lot of time. Next, we will describe in detail how to modify the relevant information in the tagging file xml.
Problem description:
For example, when I have a batch of labeled xml files, the file format is as follows:
VOC2012 x0y1115.png The VOC2007 Database PASCAL VOC2007 flickr 2233 2177 3 1 [1] Unspecified 01 0 1115 30 1145} Analysis:
It can be found that the level of the whole annotation file is according to annotation-folder/filename/. / object-name/pose/truncated/difficult/bndbox-xmin/ymin/xmax/ymax comes in such a format. Now I need to modify the category information of the annotation target (change [1] to lack), and other information can be modified in the same way. The Python code is as follows:
Solution: import osimport os.pathfrom xml.etree.ElementTree import parse Element# batch modifies the contents of xml def test (): path = ". / datasets/Annotations/" # directory where the xml file is located files = os.listdir (path) # all file names under the traversal folder for xmlFile in files: # cycle through all files path2 = ". / datasets/Annotations/" + xmlFile # locate the path of the currently processed file newStr = os.path.join (path XmlFile) dom = parse (newStr) # get the parameters in the xml file root = dom.getroot () # get the data structure for obj in root.iter ('object'): # get the name child node in the object node (here if you want to change it to something else, such as bndbox) name = obj.find (' name'). Text # get the corresponding text information # the following are custom modification rules Here, I change the content of the text message as [1] ~ [5] to lack. If name in ['[1]','[2]','[3]','[4]','[5]']: new_name = 'lack' elif name in [' [6]','[7]','[8]','[9]' '[10]']: new_name = 'black_point' elif name in [' [11]','[12]','[13]','[14]' '[15]']: new_name = 'crack' else: new_name =' Satellite_InkDrop' obj.find ('name'). Text = new_name # modify dom.write (path2 Xml_declaration=True) # Save to the specified file passif _ _ name__ ='_ _ main__': test () the information about how python batch modifies the xml file is shared here I hope that the above content will enable you to improve. If you want to learn more knowledge, please pay more attention to the editor's updates. Thank you for following the website!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.