Just returning the text of elements in xpath (python / lxml)

2024/9/23 22:35:00

I have an XML structure like this:

mytree = """
<path><to><nodes><info>1</info><info>2</info><info>3</info></nodes></to>
</path>
"""

I'm currently using xpath in python lxml to grab the nodes:

>>> from lxml import etree   
>>> info = etree.XML(mytree)   
>>> print info.xpath("/path/to/nodes/info")
[<Element info at 0x15af620>, <Element info at 0x15af940>, <Element info at 0x15af850>]  
>>> for x in info.xpath("/path/to/nodes/info"):print x.text1
2
3

This is great, but is there a cleaner way to grab just the internal texts as a list, rather than having to write the for-loop afterwards?
Something like:

print info.xpath("/path/to/nodes/info/text")

(but that doesn't work)

Answer

You can use:

print info.xpath("/path/to/nodes/info/text()")
https://en.xdnf.cn/q/71773.html

Related Q&A

Python function parameter: tuple/list

My function expects a list or a tuple as a parameter. It doesnt really care which it is, all it does is pass it to another function that accepts either a list or tuple:def func(arg): # arg is tuple or …

Python binding for C++ operator overloading

I have a class similar to the following:class A {vector<double> v;double& x(int i) { return v[2*i]; }double& y(int i) { return v[2*i+1]; }double x(int i) const { return v[2*i]; }double y(…

python script to pickle entire environment

Im working inside the Python REPL, and I want to save my work periodically. Does anybody have a script to dump all the variables I have defined? Im looking for something like this:for o in dir():f=ope…

django-endless with class based views example

Im using Class Based Views for the first time. Im having trouble understating how using class based views I would implement django-endless-pagination twitter styling paging.Could I have an example of h…

Converting adjectives and adverbs to their noun forms

I am experimenting with word sense disambiguation using wordnet for my project. As a part of the project, I would like to convert a derived adjective or an adverb form to its root noun form.For exampl…

Sending a packet over physical loopback in scapy

Ive recently discovered Scapy & it looks wonderfulIm trying to look at simple traffic over a physical loopback module / stub on my NIC.But Scapy sniff doesnt give anythingWhat Im doing to send a pa…

Python Perfect Numbers

So I am supposed to write a Python program that will identify and print all the perfect numbers in some closed interval [ 2, n ], one per line. We only have to use nested while loops/ if-else statement…

Counting consecutive 1s in NumPy array

[1, 1, 1, 0, 0, 0, 1, 1, 0, 0]I have a NumPy array consisting of 0s and 1s like above. How can I add all consecutive 1s like below? Any time I encounter a 0, I reset.[1, 2, 3, 0, 0, 0, 1, 2, 0, 0]I ca…

python 3 replacement for dircache?

Before I go reinventing the wheel, can anyone tell me if theres a drop-in (or semi-drop-in) replacement for the single-line statement:allfiles = dircache.listdir(.)

AES_128_CTR encryption by openssl and PyCrypto

Wondering the right way to convert a AES_128_CTR encryption by openssl to PyCrypto.First, I did an encryption by openssl as following:openssl enc -aes-128-ctr -in input.mp4 -out output.openssl.mp4 -K 7…