The code below returns all the “str1″‘s but I only want the first “str1” for each one of these: CrntRgstns->BrnchOfLocs->BrnchOfLoc. As in just “13A MAIN ST” not also “8 WATER ST.” Is there some type of [0] that would accomplish this? Thank you.
Current Code:
from lxml import etree f = open(r'C:UsersAndrew/Desktop/xml_file.xml') tree = etree.parse(f) for e in tree.xpath('.//CrntRgstns/following-sibling::BrnchOfLocs/BrnchOfLoc'): print(e.attrib['str1'])
Current Output:
13A MAIN ST 8 WATER ST
Desired Output:
13A MAIN ST
xml for 1 individual:
<?xml version="1.0" encoding="ISO-8859-1"?> <IAPDIndividualReport GenOn="2021-01-03"> <Indvls> <Indvl> <Info lastNm="Modafferi" firstNm="Paul" midNm="Anthony" indvlPK="2762691" actvAGReg="Y" link="https://adviserinfo.sec.gov/individual/summary/2762691"/> <OthrNms> <OthrNm lastNm="BIAMONTE" firstNm="PAUL" midNm="F"/> <OthrNm lastNm="Modafferi" firstNm="Paul" midNm="A"/> <OthrNm lastNm="Modafferi" firstNm="Paul" midNm="Anthony"/> </OthrNms> <CrntEmps> <CrntEmp orgNm="MERRILL LYNCH, PIERCE, FENNER & SMITH INCORPORATED" orgPK="7691" str1="ONE BRYANT PARK" city="NEW YORK" state="NY" cntry="United States" postlCd="10036"> <CrntRgstns> <CrntRgstn regAuth="NJ" regCat="RA" st="APPROVED" stDt="2019-11-25"/> </CrntRgstns> <BrnchOfLocs> <BrnchOfLoc str1="13A MAIN ST" city="SPARTA" state="NJ" cntry="United States" postlCd="07871"/> <BrnchOfLoc str1="8 WATER ST" city="EAGLE" state="CA" cntry="United States" postlCd="90232"/> </BrnchOfLocs> </CrntEmp> </CrntEmps>
Advertisement
Answer
To fetch first item, use [1]
(you were so close with 0!).
For example: tree.xpath('.//CrntRgstns/following-sibling::BrnchOfLocs/BrnchOfLoc[1]')
Try:
from lxml import etree f = open(r'C:UsersAndrew/Desktop/xml_file.xml') tree = etree.parse(f) for e in tree.xpath('.//CrntRgstns/following-sibling::BrnchOfLocs/BrnchOfLoc[1]'): print(e.attrib['str1'])
Output:
13A MAIN ST