0

I am using a similar xml creation code set up to the answer by @ssokolow here. The code is running and the output is correct, however the format is different than a xml that is create by say labelImg. I am not sure what is happening to cause this. The differences are:

  1. There is no <?xml version="1.0"?>. Is this a big concern?
  2. There is a blank space between <filename> and <path>
  3. <path> looks as it is indented to <filename>. I do have it indented to <annotation>
  4. Lastly, it seems that the labelImg xml indentations are "bigger". I don't think this is an issue, just something to note.

Any help would be appreciated.

import pandas
import os
import xml.etree.ElementTree as ET

csv_path = 'D:/path_to_csv/'
for csv_file in os.listdir(csv_path):
    df = pandas.read_csv(csv_path + csv_file)

    folder = 'images'
    filename = str('item' + df['tif_name'][0])
    path = 'S:/path/' + filename
    width = 1500
    height = 1500
    depth = 3

    root = ET.Element("annnotation")
    folder_elem = ET.SubElement(root, "folder").text = folder
    filename_elem = ET.SubElement(root, "filename").text = filename
    path_elem = ET.SubElement(root, "path").text = path
    source_elem = ET.SubElement(root, "source")
    database_elem = ET.SubElement(source_elem, "database").text = "Unknown"
    size_elem = ET.SubElement(root, "size")
    width_elem = ET.SubElement(size_elem, "width").text = str(width)
    height_elem = ET.SubElement(size_elem, "height").text = str(height)
    depth_elem = ET.SubElement(size_elem, "depth").text = str(depth)
    segmented_elem = ET.SubElement(root, "segmented").text = str(0)

    # Appending Function goes here

    tree = ET.ElementTree(root)
    tree.write('C:output_path/item' + str(csv_file[:-4] + '.xml'))

labelImg xml enter image description here

My code xml enter image description here

Binx
  • 382
  • 7
  • 22
  • If you are on Python 3.9, try the `indent()` function. See https://stackoverflow.com/a/63373633/407651. – mzjn May 19 '21 at 17:40
  • @mzjn unfortunately I am using python 3.7. – Binx May 19 '21 at 17:59
  • 1
    OK. Maybe you can use one of the other answers to "Pretty printing XML in Python" (there are more than 20). – mzjn May 19 '21 at 18:02
  • 1
    using `write(..., xml_declaration=True)` you should get `` but indentations are not important for computer/program to load data - they are important only for human. It may need to use `lxml` which has `tostring(..., pretty_print=True)`. OR `beutifulsoup` which has `soup.prettify()` – furas May 19 '21 at 18:07
  • 1
    for small file you can even use `f-string` to genrate it. Create string with all xml tags and indentations and with elements like `{{width}}` to generate string with data - and save it as any text file using standard `open()`,`write()`, `close()`. And it – furas May 19 '21 at 18:13
  • @mzjn if you have link to some question which describe how to do it then close it. – furas May 19 '21 at 18:15
  • @furas is the `xml_declaration` even needed then? And yes, I have seen the `f-string` approach. Good to know about the indentations! – Binx May 19 '21 at 18:18
  • 1
    The XML declaration is recommended, but not mandatory. https://stackoverflow.com/q/7007427/407651 – mzjn May 19 '21 at 18:20
  • Cheers! appreciate the help. Welcome to close my question if you think it is necessary. – Binx May 19 '21 at 18:23
  • Oh how interesting. I made the `.xml` window full screen and the indentation "problem" went away. – Binx May 19 '21 at 19:38

0 Answers0