Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the number of elements in element tree in python?

I am new to element tree,here i am trying to find the number of elements in the element tree.

from lxml import etree 
root = etree.parse(open("file.xml",'r'))

is there any way to find the total count of the elements in root?

like image 546
mariz Avatar asked Jul 01 '16 06:07

mariz


People also ask

What is Etree in Python?

The xml.etree.ElementTree module implements a simple and efficient API for parsing and creating XML data. Changed in version 3.3: This module will use a fast implementation whenever available.

How do I count the number of tags in XML?

Count the XML elements (XPath)newXPath(); NodeList nodes = (NodeList) xpath. evaluate("//staff", doc, XPathConstants. NODESET); int count = nodes. getLength();

How do you access XML elements in Python?

To read an XML file using ElementTree, firstly, we import the ElementTree class found inside xml library, under the name ET (common convension). Then passed the filename of the xml file to the ElementTree. parse() method, to enable parsing of our xml file. Then got the root (parent tag) of our xml file using getroot().

How to find the number of elements in a list in Python?

To find the number of elements in a list, you can use the built-in len() function. Len() function can be used to find the length of objects like string, bytes, tuple, list, etc. Let’s see an example to find the count of elements in a list.

How many unique elements are there in a list in Python?

Number of elements in the list: 8 Number of unique elements in the list: 5 We can see that list_d has a total of 8 elements, among which 5 are unique. List of Lists using len() In the introduction, we saw that elements of lists can be of different data types. However, lists can have, in turn, lists as their elements. For example:

How to count the number of elements in a list?

The approaches vary whether you want to count nested lists as one element or all the elements in the nested lists, or whether you're only interested in unique elements, and similar. The most straightforward way to get the number of elements in a list is to use the Python built-in function len (). Let's look at the following example:

How to count occurrences of an element in a list python?

Python | Count occurrences of an element in a list. Given a list in Python and a number x, count number of occurrences of x in the given list. Examples: Input : lst = [15, 6, 7, 10, 12, 20, 10, 28, 10] x = 10 Output : 3 10 appears three times in given list.


4 Answers

Find all the target elements (there are some ways to do this), and then use built-in function len() to get the count. For example, if you mean to count only direct child elements of root :

from lxml import etree 
doc = etree.parse("file.xml")
root = doc.getroot()

result = len(root.getchildren())

or, if you mean to count all elements within root element :

result = len(root.xpath(".//*"))
like image 186
har07 Avatar answered Oct 20 '22 03:10

har07


You don't have to load all the nodes into a list, you can use sum and lazily iterate:

from lxml import etree 
root = etree.parse(open("file.xml",'r'))
count = sum(1 for _ in root.iter("*"))
like image 42
Padraic Cunningham Avatar answered Oct 20 '22 02:10

Padraic Cunningham


Another way to get the number of subelements:

len(list(root))
like image 11
ThomasW Avatar answered Oct 20 '22 01:10

ThomasW


you can find the count of each element like this:

from lxml import objectify

file_root = objectify.parse('path/to/file').getroot()
file_root.countchildren()  # root's element count
file_root.YourElementName.countchildren()  # count of children in any element
like image 3
SanD Avatar answered Oct 20 '22 01:10

SanD