no classification
no tag
no datas
posted on 2024-12-02 22:10 read(776) comment(0) like(29) collect(2)
In my xml file, I have nodes like this:
<waitingJobs idList="J03ac2db8 J03ac2fb0"/>
I know how to use .findall
to search for attributes but now, it looks like I would need to use regular expressions because I can't just use
root.findall('./[@attrib='value']')
I'd have to use
root.findall('./[@attrib='*value*']')
QUESTION
xml.etree
?lxml
?Unfortunately, things like contains()
and starts-with()
are not supported by xml.etree.ElementTree
built-in library. You can manually check the attribute, finding all waitingJobs
and using .attrib
to get to the idList
value:
import xml.etree.ElementTree as ET
data = """<jobs>
<waitingJobs idList="J03ac2db8 J03ac2fb0"/>
</jobs>
"""
root = ET.fromstring(data)
value = 'J03ac2db8'
print([elm for elm in root.findall(".//waitingJobs[@idList]")
if value in elm.attrib["idList"]])
With lxml.etree
, you can use xpath()
method and contains()
function:
import lxml.etree as ET
data = """<jobs>
<waitingJobs idList="J03ac2db8 J03ac2fb0"/>
</jobs>
"""
root = ET.fromstring(data)
value = 'J03ac2db8'
print(root.xpath(".//waitingJobs[contains(@idList, '%s')]" % value))
Author:qs
link:http://www.pythonblackhole.com/blog/article/247242/8778639f30284bc02dec/
source:python black hole net
Please indicate the source for any form of reprinting. If any infringement is discovered, it will be held legally responsible.
name:
Comment content: (supports up to 255 characters)
Copyright © 2018-2021 python black hole network All Rights Reserved All rights reserved, and all rights reserved.京ICP备18063182号-7
For complaints and reports, and advertising cooperation, please contact vgs_info@163.com or QQ3083709327
Disclaimer: All articles on the website are uploaded by users and are only for readers' learning and communication use, and commercial use is prohibited. If the article involves pornography, reactionary, infringement and other illegal information, please report it to us and we will delete it immediately after verification!