Re: [lxml] attrname - get value of attrname
Date: Tue, 4 Oct 2016 Subject: Re: [lxml] attrname - get value of attrname Message-ID: <OFB7C4D3DA.6DB2EE2B-ONC1258042.0021FC9D-C1258042.0023169A@LBBW.de
Content-Type: text/plain;
The result of tree.xpath('//meeting/@*') is already a list of attribute
*values*. These are "smart strings" i.e. strings with additional niceties (like the .attrname attribute, see http://lxml.de/xpathxslt.html#xpath) and should just work in your usual string contexts.
E.g.
root = etree.fromstring("""<root><x a="a attr"/><y b="b attr"/> </root>""") root.xpath('//@*') ['a attr', 'b attr'] root.xpath('//@*')[0] 'a attr' root.xpath('//@*')[0].attrname 'a' root.xpath('//@*')[1] + " is nice to have" 'b attr is nice to have'
If need be you can always convert them to plain strings using unicode() or str(),
Thank you i definitely got a working version. I can't help thinking though i have not optimally used lxml.
A key thing is, well 2; other than with my eyes what is a good way with lxml to check that the values i am obtainjng are correct especially when as i am going over multiple files. Should I be randomly generating a file in reverse and comparing to the original? Second the xml is only 4 levels deep with 5 main nodes comprising these levels. However each node is attribute heavy (10+). While i have used getchildren etc to reduce looping down i still have looped down to the main extent. Each line of my csv output contains details from each node. So abstracted thats my flow start at root grab a few and at each level repeat and then create a line for csv written later. How do you approach these tasks? Thanks Sayth
participants (1)
-
Sayth Renshaw