Home > Software design >  How to extract mutiple xml tags in elementtree in Python?
How to extract mutiple xml tags in elementtree in Python?

Time:10-05

My xml data looks like below. I am trying to extract "weighted_avg" and "change_in_wtd_avg" for "CUTTER/CANNER" and "FROZEN CUTTER/CANNER". For this I have below code, where I can print only data from either "CUTTER/CANNER" or "FROZEN CUTTER/CANNER". I am not sure how can I get the data from "CUTTER/CANNER" and "FROZEN CUTTER/CANNER" in the same output. I am new to xml and python ElementTree.

XML Data

b'<?xml version=\'1.0\' encoding=\'UTF-8\'?><results exportTime="2021-01-03 13:16:25 CDT"><report label="National" slug="LM_XB001"><record report_date="01/24/2009" narrative="null"><report label="Current Cutout Value/Change"><record current_cutout_value="232.67" change_cutout_value="-.03"/></report><report label="Items"><record item_desc="90% lean  " current_price="276.93" current_value="154.64" change_value=".00"/><record item_desc="100% lean inside round  " current_price="563.93" current_value="13.08" change_value=".09"/><record item_desc="100% lean, flats and eyes  " current_price="429.52" current_value="9.96" change_value=".00"/><record item_desc="100% lean, S.P.B.  " current_price="431.54" current_value="21.58" change_value=".00"/><record item_desc="Chuck Tender  " current_price="311.27" current_value="3.11" change_value="-.02"/><record item_desc="Knuckle  4-7 lbs." current_price="323.60" current_value="8.19" change_value=".01"/><record item_desc="Tenderloin  2-3 lbs." current_price="442.55" current_value="2.35" change_value="-.06"/><record item_desc="Tenderloin  3-4 lbs." current_price="663.86" current_value="4.91" change_value=".00"/><record item_desc="Ribeye Roll  4-6 lbs." current_price="405.00" current_value="4.25" change_value=".00"/><record item_desc="Ribeye Roll  6-8 lbs." current_price="392.05" current_value="2.90" change_value="-.06"/><record item_desc="Ribeye Roll  8up lbs." current_price="435.00" current_value="3.18" change_value=".00"/><record item_desc="Flank Steak  " current_price="367.34" current_value="1.54" change_value=".01"/><record item_desc="Kidney, Edible  " current_price="40.00" current_value=".18" change_value=".00"/><record item_desc="Fat, inedible  " current_price="44.72" current_value="1.04" change_value=".00"/><record item_desc="Bone  " current_price="7.32" current_value="1.76" change_value=".00"/></report><report label="Current Volume"><record boner_volume_loads="17.31" cutter_volume_loads="4.11" bbcc_volume_loads="19.47" lean_volume_loads="19.09" frozen_volume_loads="4.61" boner_volume_pounds="692,580" cutter_volume_pounds="164,565" bbcc_volume_pounds="778,700" lean_volume_pounds="763,409" frozen_volume_pounds="184,527"/></report><report label="BONER/BREAKER"><record item_desc="Rib, ribeye roll, 6-8 lbs. (112)" number_trades="11" total_pounds="67,507" weighted_avg="496.91" change_in_wtd_avg="4.35"/><record item_desc="Rib, ribeye roll, 8-10 lbs. (112)" number_trades="32" total_pounds="45,014" weighted_avg="563.10" change_in_wtd_avg="-8.29"/><record item_desc="Rib, ribeye roll, 10-up lbs. (112)" number_trades="52" total_pounds="110,549" weighted_avg="523.10" change_in_wtd_avg="-6.89"/><record item_desc="Rib, ribeye roll-lip-on, 8-dn lbs. (112A)" number_trades="9" total_pounds="14,619" weighted_avg="745.28" change_in_wtd_avg=".72"/><record item_desc="Rib, ribeye roll-lip-on, 8-up lbs. (112A)" number_trades="18" total_pounds="23,518" weighted_avg="590.82" change_in_wtd_avg="-8.21"/><record item_desc="Chuck, boneless 85% ( )" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Chuck, brisket (120)" number_trades="13" total_pounds="21,366" weighted_avg="320.60" change_in_wtd_avg="-.72"/><record item_desc="Round, top inside, 10-dn lbs. (168)" number_trades="13" total_pounds="43,841" weighted_avg="288.11" change_in_wtd_avg="-3.57"/><record item_desc="Round, top inside, 10-up lbs. (168)" number_trades="5" total_pounds="4,188" weighted_avg="378.20" change_in_wtd_avg=".00"/><record item_desc="Round, top inside c-off, 8-10 lbs. (169A)" number_trades="null" total_pounds="null" weighted_avg="null" change_in_wtd_avg="null"/><record item_desc="Round, top inside c-off, 10-14 lbs. (169A)" number_trades="14" total_pounds="160,462" weighted_avg="426.16" change_in_wtd_avg=".00"/><record item_desc="Round, top inside c-off, 14-up lbs. (169A)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Round, outside round (171B)" number_trades="6" total_pounds="22,900" weighted_avg="432.18" change_in_wtd_avg=".00"/><record item_desc="Round, eye of round (171C)" number_trades="37" total_pounds="69,061" weighted_avg="418.83" change_in_wtd_avg="1.02"/><record item_desc="Loin, Semi-Bnls Short Loin, 13-dn lbs. ( )" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Loin, Semi-Bnls Short Loin, 13-up lbs. ( )" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Loin, strip, bnls, 7-9 lbs. (180)" number_trades="5" total_pounds="3,232" weighted_avg="337.92" change_in_wtd_avg=".53"/><record item_desc="Loin, strip, bnls, 9-up lbs. (180)" number_trades="28" total_pounds="36,542" weighted_avg="364.57" change_in_wtd_avg="-8.85"/><record item_desc="Loin, sirloin butt (182)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Loin, top sirloin butt (184)" number_trades="17" total_pounds="29,315" weighted_avg="316.51" change_in_wtd_avg=".57"/><record item_desc="Loin, bottom sirloin butt flap, bnls. (185A)" number_trades="13" total_pounds="36,343" weighted_avg="376.82" change_in_wtd_avg="9.17"/><record item_desc="Loin, butt tender, peeled (191A)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/></report><report label="CUTTER/CANNER"><record item_desc="Rib, ribeye roll, 4-6 lbs. (112)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Rib, ribeye roll, 6-8 lbs. (112)" number_trades="42" total_pounds="164,565" weighted_avg="389.13" change_in_wtd_avg="-11.84"/><record item_desc="Rib, ribeye roll, 8-up lbs. (112)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/></report><report label="100% LEAN"><record item_desc="Inside round - Combo" number_trades="16" total_pounds="101,110" weighted_avg="561.59" change_in_wtd_avg="1.95"/><record item_desc="Inside round - Boxed" number_trades="27" total_pounds="40,350" weighted_avg="569.80" change_in_wtd_avg="8.22"/><record item_desc="Outside round" number_trades="4" total_pounds="33,558" weighted_avg="429.74" change_in_wtd_avg="null"/><record item_desc="Eye of round" number_trades="19" total_pounds="30,408" weighted_avg="435.41" change_in_wtd_avg="-.74"/><record item_desc="Flats and eyes - Combo" number_trades="14" total_pounds="206,885" weighted_avg="428.61" change_in_wtd_avg="-.30"/><record item_desc="Flats and eyes - Boxed" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Striploin" number_trades="81" total_pounds="243,034" weighted_avg="466.56" change_in_wtd_avg="-1.73"/><record item_desc="S.P.B. - Combo" number_trades="12" total_pounds="108,064" weighted_avg="431.54" change_in_wtd_avg="-.06"/><record item_desc="S.P.B. - Boxed" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/></report><report label="B/B and C/C"><record item_desc="Chuck, chuck tender (116B)" number_trades="30" total_pounds="73,521" weighted_avg="311.27" change_in_wtd_avg="-2.08"/><record item_desc="Round, knuckle, peeled (167A)" number_trades="39" total_pounds="199,293" weighted_avg="323.60" change_in_wtd_avg=".28"/><record item_desc="Loin, tenderloin, 2-3 lbs. (190)" number_trades="6" total_pounds="8,983" weighted_avg="447.16" change_in_wtd_avg="-12.52"/><record item_desc="Loin, tenderloin, 3-4 lbs. (190)" number_trades="43" total_pounds="69,979" weighted_avg="663.86" change_in_wtd_avg=".20"/><record item_desc="Loin, tenderloin, 4-5 lbs. (190)" number_trades="169" total_pounds="212,729" weighted_avg="773.68" change_in_wtd_avg="-4.71"/><record item_desc="Loin, tenderloin, 5-up lbs. (190)" number_trades="37" total_pounds="43,056" weighted_avg="617.55" change_in_wtd_avg="13.85"/><record item_desc="Flank, flank steak (193)" number_trades="82" total_pounds="116,533" weighted_avg="390.93" change_in_wtd_avg="1.18"/><record item_desc="Flank, rough ()" number_trades="44" total_pounds="54,606" weighted_avg="322.56" change_in_wtd_avg="-5.70"/></report><report label="FROZEN BONER/BREAKER"><record item_desc="Rib, ribeye roll, 6-8 lbs. (112)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Rib, ribeye roll, 8-10 lbs. (112)" number_trades="6" total_pounds="16,005" weighted_avg="473.41" change_in_wtd_avg=".00"/><record item_desc="Rib, ribeye roll, 10-up lbs. (112)" number_trades="null" total_pounds="null" weighted_avg="null" change_in_wtd_avg="null"/><record item_desc="Rib, ribeye roll-lip-on, 8-dn lbs. (112A)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Rib, ribeye roll-lip-on, 8-up lbs. (112A)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Round, top inside c-off, 8-10 lbs. (169A)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Round, top inside c-off, 10-14 lbs. (169A)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Round, top inside c-off, 14-up lbs. (169A)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Loin, strip, bnls, 7-9 lbs. (180)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Loin, strip, bnls, 9-up lbs. (180)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/></report><report label="FROZEN CUTTER/CANNER"><record item_desc="Rib, ribeye roll, 4-6 lbs. (112)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Rib, ribeye roll, 6-8 lbs. (112)" number_trades="5" total_pounds="85,800" weighted_avg="397.65" change_in_wtd_avg="-2.26"/><record item_desc="Rib, ribeye roll, 8-up lbs. (112)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/></report><report label="FROZEN B/B and C/C"><record item_desc="Loin, tenderloin, 2-3 lbs. (190)" number_trades="null" total_pounds="null" weighted_avg="null" change_in_wtd_avg="null"/><record item_desc="Loin, tenderloin, 3-4 lbs. (190)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Loin, tenderloin, 4-5 lbs. (190)" number_trades="null" total_pounds="null" weighted_avg="null" change_in_wtd_avg="null"/><record item_desc="Loin, tenderloin, 5-up lbs. (190)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Flank, flank steak (193)" number_trades="10" total_pounds="77,922" weighted_avg="332.06" change_in_wtd_avg="-.32"/></report></record></report></results>'

Code:

import xml.etree.ElementTree as ET

r1 = requests.get(xml link)
root1 = ET.fromstring(r1.content)

    for child in root1.find(".//report[@label='CUTTER/CANNER']"):
        print(child.attrib.get('weighted_avg'),child.attrib.get('change_in_wtd_avg'))

Expected Output:

null     null
399.75   .65
457.78    .00
.00      null
413.49   3.73
.00      null

CodePudding user response:

I think you should run for-loop two times

for child in root1.find(".//report[@label='CUTTER/CANNER']"):
    print(child.attrib.get('weighted_avg'), child.attrib.get('change_in_wtd_avg'))    

for child in root1.find(".//report[@label='FROZEN CUTTER/CANNER']"):
    print(child.attrib.get('weighted_avg'), child.attrib.get('change_in_wtd_avg'))    

and it shouldn't repeate values.

But I would use findall() instead of find() to get all elements if there is more 'CUTTER/CANNER' and more 'FROZEN CUTTER/CANNER'

for item in root1.findall(".//report[@label='CUTTER/CANNER']"):
    for child in item:
        print(child.attrib.get('weighted_avg'), child.attrib.get('change_in_wtd_avg'))

for item in root1.findall(".//report[@label='FROZEN CUTTER/CANNER']"):
    for child in item:
        print(child.attrib.get('weighted_avg'), child.attrib.get('change_in_wtd_avg'))

Using module lxml you could use function xpath() instead of find() and it can use or or contains()

(doc for xml shows XPath support and it doesn't support all XPath functions)

for item in root1.xpath(".//report[@label='CUTTER/CANNER' or @label='FROZEN CUTTER/CANNER']"):
    for child in item:
        print(child.attrib.get('weighted_avg'), child.attrib.get('change_in_wtd_avg'))
for item in root1.xpath(".//report[contains(@label, 'CUTTER/CANNER')]"):
    for child in item:
        print(child.attrib.get('weighted_avg'), child.attrib.get('change_in_wtd_avg'))

Minimal working example

content = b'<?xml version=\'1.0\' encoding=\'UTF-8\'?><results exportTime="2021-01-03 13:16:25 CDT"><report label="National" slug="LM_XB001"><record report_date="01/24/2009" narrative="null"><report label="Current Cutout Value/Change"><record current_cutout_value="232.67" change_cutout_value="-.03"/></report><report label="Items"><record item_desc="90% lean  " current_price="276.93" current_value="154.64" change_value=".00"/><record item_desc="100% lean inside round  " current_price="563.93" current_value="13.08" change_value=".09"/><record item_desc="100% lean, flats and eyes  " current_price="429.52" current_value="9.96" change_value=".00"/><record item_desc="100% lean, S.P.B.  " current_price="431.54" current_value="21.58" change_value=".00"/><record item_desc="Chuck Tender  " current_price="311.27" current_value="3.11" change_value="-.02"/><record item_desc="Knuckle  4-7 lbs." current_price="323.60" current_value="8.19" change_value=".01"/><record item_desc="Tenderloin  2-3 lbs." current_price="442.55" current_value="2.35" change_value="-.06"/><record item_desc="Tenderloin  3-4 lbs." current_price="663.86" current_value="4.91" change_value=".00"/><record item_desc="Ribeye Roll  4-6 lbs." current_price="405.00" current_value="4.25" change_value=".00"/><record item_desc="Ribeye Roll  6-8 lbs." current_price="392.05" current_value="2.90" change_value="-.06"/><record item_desc="Ribeye Roll  8up lbs." current_price="435.00" current_value="3.18" change_value=".00"/><record item_desc="Flank Steak  " current_price="367.34" current_value="1.54" change_value=".01"/><record item_desc="Kidney, Edible  " current_price="40.00" current_value=".18" change_value=".00"/><record item_desc="Fat, inedible  " current_price="44.72" current_value="1.04" change_value=".00"/><record item_desc="Bone  " current_price="7.32" current_value="1.76" change_value=".00"/></report><report label="Current Volume"><record boner_volume_loads="17.31" cutter_volume_loads="4.11" bbcc_volume_loads="19.47" lean_volume_loads="19.09" frozen_volume_loads="4.61" boner_volume_pounds="692,580" cutter_volume_pounds="164,565" bbcc_volume_pounds="778,700" lean_volume_pounds="763,409" frozen_volume_pounds="184,527"/></report><report label="BONER/BREAKER"><record item_desc="Rib, ribeye roll, 6-8 lbs. (112)" number_trades="11" total_pounds="67,507" weighted_avg="496.91" change_in_wtd_avg="4.35"/><record item_desc="Rib, ribeye roll, 8-10 lbs. (112)" number_trades="32" total_pounds="45,014" weighted_avg="563.10" change_in_wtd_avg="-8.29"/><record item_desc="Rib, ribeye roll, 10-up lbs. (112)" number_trades="52" total_pounds="110,549" weighted_avg="523.10" change_in_wtd_avg="-6.89"/><record item_desc="Rib, ribeye roll-lip-on, 8-dn lbs. (112A)" number_trades="9" total_pounds="14,619" weighted_avg="745.28" change_in_wtd_avg=".72"/><record item_desc="Rib, ribeye roll-lip-on, 8-up lbs. (112A)" number_trades="18" total_pounds="23,518" weighted_avg="590.82" change_in_wtd_avg="-8.21"/><record item_desc="Chuck, boneless 85% ( )" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Chuck, brisket (120)" number_trades="13" total_pounds="21,366" weighted_avg="320.60" change_in_wtd_avg="-.72"/><record item_desc="Round, top inside, 10-dn lbs. (168)" number_trades="13" total_pounds="43,841" weighted_avg="288.11" change_in_wtd_avg="-3.57"/><record item_desc="Round, top inside, 10-up lbs. (168)" number_trades="5" total_pounds="4,188" weighted_avg="378.20" change_in_wtd_avg=".00"/><record item_desc="Round, top inside c-off, 8-10 lbs. (169A)" number_trades="null" total_pounds="null" weighted_avg="null" change_in_wtd_avg="null"/><record item_desc="Round, top inside c-off, 10-14 lbs. (169A)" number_trades="14" total_pounds="160,462" weighted_avg="426.16" change_in_wtd_avg=".00"/><record item_desc="Round, top inside c-off, 14-up lbs. (169A)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Round, outside round (171B)" number_trades="6" total_pounds="22,900" weighted_avg="432.18" change_in_wtd_avg=".00"/><record item_desc="Round, eye of round (171C)" number_trades="37" total_pounds="69,061" weighted_avg="418.83" change_in_wtd_avg="1.02"/><record item_desc="Loin, Semi-Bnls Short Loin, 13-dn lbs. ( )" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Loin, Semi-Bnls Short Loin, 13-up lbs. ( )" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Loin, strip, bnls, 7-9 lbs. (180)" number_trades="5" total_pounds="3,232" weighted_avg="337.92" change_in_wtd_avg=".53"/><record item_desc="Loin, strip, bnls, 9-up lbs. (180)" number_trades="28" total_pounds="36,542" weighted_avg="364.57" change_in_wtd_avg="-8.85"/><record item_desc="Loin, sirloin butt (182)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Loin, top sirloin butt (184)" number_trades="17" total_pounds="29,315" weighted_avg="316.51" change_in_wtd_avg=".57"/><record item_desc="Loin, bottom sirloin butt flap, bnls. (185A)" number_trades="13" total_pounds="36,343" weighted_avg="376.82" change_in_wtd_avg="9.17"/><record item_desc="Loin, butt tender, peeled (191A)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/></report><report label="CUTTER/CANNER"><record item_desc="Rib, ribeye roll, 4-6 lbs. (112)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Rib, ribeye roll, 6-8 lbs. (112)" number_trades="42" total_pounds="164,565" weighted_avg="389.13" change_in_wtd_avg="-11.84"/><record item_desc="Rib, ribeye roll, 8-up lbs. (112)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/></report><report label="100% LEAN"><record item_desc="Inside round - Combo" number_trades="16" total_pounds="101,110" weighted_avg="561.59" change_in_wtd_avg="1.95"/><record item_desc="Inside round - Boxed" number_trades="27" total_pounds="40,350" weighted_avg="569.80" change_in_wtd_avg="8.22"/><record item_desc="Outside round" number_trades="4" total_pounds="33,558" weighted_avg="429.74" change_in_wtd_avg="null"/><record item_desc="Eye of round" number_trades="19" total_pounds="30,408" weighted_avg="435.41" change_in_wtd_avg="-.74"/><record item_desc="Flats and eyes - Combo" number_trades="14" total_pounds="206,885" weighted_avg="428.61" change_in_wtd_avg="-.30"/><record item_desc="Flats and eyes - Boxed" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Striploin" number_trades="81" total_pounds="243,034" weighted_avg="466.56" change_in_wtd_avg="-1.73"/><record item_desc="S.P.B. - Combo" number_trades="12" total_pounds="108,064" weighted_avg="431.54" change_in_wtd_avg="-.06"/><record item_desc="S.P.B. - Boxed" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/></report><report label="B/B and C/C"><record item_desc="Chuck, chuck tender (116B)" number_trades="30" total_pounds="73,521" weighted_avg="311.27" change_in_wtd_avg="-2.08"/><record item_desc="Round, knuckle, peeled (167A)" number_trades="39" total_pounds="199,293" weighted_avg="323.60" change_in_wtd_avg=".28"/><record item_desc="Loin, tenderloin, 2-3 lbs. (190)" number_trades="6" total_pounds="8,983" weighted_avg="447.16" change_in_wtd_avg="-12.52"/><record item_desc="Loin, tenderloin, 3-4 lbs. (190)" number_trades="43" total_pounds="69,979" weighted_avg="663.86" change_in_wtd_avg=".20"/><record item_desc="Loin, tenderloin, 4-5 lbs. (190)" number_trades="169" total_pounds="212,729" weighted_avg="773.68" change_in_wtd_avg="-4.71"/><record item_desc="Loin, tenderloin, 5-up lbs. (190)" number_trades="37" total_pounds="43,056" weighted_avg="617.55" change_in_wtd_avg="13.85"/><record item_desc="Flank, flank steak (193)" number_trades="82" total_pounds="116,533" weighted_avg="390.93" change_in_wtd_avg="1.18"/><record item_desc="Flank, rough ()" number_trades="44" total_pounds="54,606" weighted_avg="322.56" change_in_wtd_avg="-5.70"/></report><report label="FROZEN BONER/BREAKER"><record item_desc="Rib, ribeye roll, 6-8 lbs. (112)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Rib, ribeye roll, 8-10 lbs. (112)" number_trades="6" total_pounds="16,005" weighted_avg="473.41" change_in_wtd_avg=".00"/><record item_desc="Rib, ribeye roll, 10-up lbs. (112)" number_trades="null" total_pounds="null" weighted_avg="null" change_in_wtd_avg="null"/><record item_desc="Rib, ribeye roll-lip-on, 8-dn lbs. (112A)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Rib, ribeye roll-lip-on, 8-up lbs. (112A)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Round, top inside c-off, 8-10 lbs. (169A)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Round, top inside c-off, 10-14 lbs. (169A)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Round, top inside c-off, 14-up lbs. (169A)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Loin, strip, bnls, 7-9 lbs. (180)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Loin, strip, bnls, 9-up lbs. (180)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/></report><report label="FROZEN CUTTER/CANNER"><record item_desc="Rib, ribeye roll, 4-6 lbs. (112)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Rib, ribeye roll, 6-8 lbs. (112)" number_trades="5" total_pounds="85,800" weighted_avg="397.65" change_in_wtd_avg="-2.26"/><record item_desc="Rib, ribeye roll, 8-up lbs. (112)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/></report><report label="FROZEN B/B and C/C"><record item_desc="Loin, tenderloin, 2-3 lbs. (190)" number_trades="null" total_pounds="null" weighted_avg="null" change_in_wtd_avg="null"/><record item_desc="Loin, tenderloin, 3-4 lbs. (190)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Loin, tenderloin, 4-5 lbs. (190)" number_trades="null" total_pounds="null" weighted_avg="null" change_in_wtd_avg="null"/><record item_desc="Loin, tenderloin, 5-up lbs. (190)" number_trades="0" total_pounds="0" weighted_avg=".00" change_in_wtd_avg="null"/><record item_desc="Flank, flank steak (193)" number_trades="10" total_pounds="77,922" weighted_avg="332.06" change_in_wtd_avg="-.32"/></report></record></report></results>'

def version1(content):
    print('--- version1 ---')
    
    import xml.etree.ElementTree as ET

    #r1 = requests.get(xml link)
    #root1 = ET.fromstring(r1.content)

    root1 = ET.fromstring(content)

    for item in root1.findall(".//report[@label='CUTTER/CANNER']"):
        for child in item:
            print(child.attrib.get('weighted_avg'), child.attrib.get('change_in_wtd_avg'))

    for item in root1.findall(".//report[@label='FROZEN CUTTER/CANNER']"):
        for child in item:
            print(child.attrib.get('weighted_avg'), child.attrib.get('change_in_wtd_avg'))


def version2(content):
    print('--- version2 ---')

    import lxml.etree as ET

    #r1 = requests.get(xml link)
    #root1 = ET.fromstring(r1.content)

    root1 = ET.fromstring(content)

    for item in root1.xpath(".//report[contains(@label, 'CUTTER/CANNER')]"):
        for child in item:
            print(child.attrib.get('weighted_avg'), child.attrib.get('change_in_wtd_avg'))


def version3(content):
    print('--- version3 ---')

    import lxml.etree as ET

    #r1 = requests.get(xml link)
    #root1 = ET.fromstring(r1.content)

    root1 = ET.fromstring(content)

    for item in root1.xpath(".//report[@label='CUTTER/CANNER' or @label='FROZEN CUTTER/CANNER']"):
        for child in item:
            print(child.attrib.get('weighted_avg'), child.attrib.get('change_in_wtd_avg'))

# --- main ---

version1(content)
version2(content)
version3(content)
  • Related