How Python uses lxml to read and write XML tutorials

Source: Internet
Author: User
This article mainly for you in detail the Python use lxml read and write XML format files, with a certain reference value, interested in small partners can refer to

It is very convenient to use the lxml package to convert the JSON to an XML file before converting the dataset format.

1. Writing XML files

A) using etree and objectify


From lxml import etree, Objectifye = objectify. Elementmaker (annotate=false) Anno_tree = E.annotation (  e.folder (' voc2014_instance '),  e.filename ("test.jpg "),  E.source (    e.database (' Coco '),    e.annotation (' Coco '),    e.image (' Coco '),    e.url ("/HTTP/ Test.jpg ")  ,  e.size (e.width),    e.height (    3  ), E.depth ( 0), etree. ElementTree (Anno_tree). Write ("Text.xml", Pretty_print=true)

The contents of the output Test.xml file are as follows:

"

If you need to add other labels on the basis of Anno_tree, use append:


E2 = objectify. Elementmaker (annotate=false) anno_tree2 = E2.object (  e.name ("person"),  E.bndbox (    e.xmin (+),    E.ymin (E.xmax), E.ymax (+),  e.difficult (0)) Anno_tree.append (ANNO_TREE2)

The above output becomes:


<annotation> <folder>VOC2014_instance/person</folder> <filename>test.jpg</filename > <source>  <database>COCO</database>  <annotation>COCO</annotation>  <image>COCO</image>  <url>http://test.jpg</url> </source> <size>  <width>800</width>  

b) using etree and subelement


annotation = etree. Element ("annotation") etree. subelement (Annotation, "folder"). Text = "Voc2014_instance" etree. subelement (annotation, "filename"). Text = "Test.jpg" Source = etree. subelement (annotation, "source") etree. subelement (source, "database"). Text = "COCO" etree. subelement (source, "annotation"). Text = "COCO" etree. subelement (source, "image"). Text = "COCO" etree. subelement (source, "url"). Text = "Http://test.jpg" size = etree. subelement (annotation, "size") etree. subelement (Size, "width"). Text = ' 800 ' # must be used with Stringetree. subelement (Size, "height"). Text = ' etree '. subelement (size, "depth"). Text = ' 3 ' etree. subelement (annotation, "segmented"). Text = ' 0 ' key_object = etree. subelement (Annotation, "object") etree. Subelement (Key_object, "name"). Text = "person" Bndbox = etree. Subelement (Key_object, "Bndbox") etree. Subelement (Bndbox, "xmin"). Text = str (+) etree. Subelement (Bndbox, "ymin"). Text = str (etree). Subelement (Bndbox, "Xmax"). Text = str etree. Subelement (Bndbox, "Ymax"). Text = str (+) etree. SUbelement (Key_object, "difficult"). Text = ' 0 ' doc = etree. ElementTree (annotation) doc.write (open ("Test.xml", "W"), Pretty_print=true)

2. Read XML

Here you can use XPath to extract the value of the desired element directly. For example, to get the x, y coordinates of the Test.xml file above:


Tree = Etree.parse ("Test.xml") # get Bboxfor bbox in Tree.xpath ('//bndbox '):  # Gets the contents of the Bndbox element for  corner in bbox.ge Tchildren (): # facilitates Bndbox element under sub-element    print Corner.text  # string type

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.