Skip to content Skip to sidebar Skip to footer

How To Get Specific Values From A Xml File Into Csv File Using Python?

I am trying to extract object, xmin, ymin, xmax and xmax value of every object tag there is. XML Plates_Number

Solution 1:

code :

import xml.etree.ElementTree as ETroot= ET.parse('file.xml').getroot()


for type_tag in root.findall('object'):
    name = type_tag.find('name').textxmin= type_tag.find('bndbox/xmin').textymin= type_tag.find('bndbox/ymin').textxmax= type_tag.find('bndbox/xmax').textymax= type_tag.find('bndbox/ymax').text

    print([name,xmin,ymin,xmax,ymax])

output:

['2', '40', '1', '69', '42']['10', '67', '3', '101', '43']['1', '122', '2', '153', '45']['10', '151', '3', '183', '44']['2', '186', '4', '216', '47']['5', '214', '5', '245', '46']

Solution 2:

If you can use BeautifulSoup, you could use

from bs4 import BeautifulSoup
soup = BeautifulSoup(input_xml_string)
tgs = soup.find_all('object', 'xml')
l = [(i.find('name').string, i.xmin.string, i.ymin.string, i.xmax.string, i.ymax.string) for i in tgs]

where input_xml_string is the input xml in string form.

soup would be a BeautifulSoup object which is a representation of the xml tree.

An xml parser is used.

Then the find_all() function is used to find all the <object> tags in the xml. The result is stored in tgs.

Now from the elements in tgs, which would be children tags of <object>, we select the tags we need, which are Tag objects, and get their values using their string attribute.

We could have accessed the value in name using its string attribute but name is the name of an attribute of the Tag class. So we first used find() to get the <name> child of <object> and then we got its content.

Now if we print the values in l,

for i in l:
    print(i)

we would get,

('2', '40', '1', '69', '42')
('10', '67', '3', '101', '43')
('1', '122', '2', '153', '45')
('10', '151', '3', '183', '44')
('2', '186', '4', '216', '47')
('5', '214', '5', '245', '46')

Post a Comment for "How To Get Specific Values From A Xml File Into Csv File Using Python?"