gpt4 book ai didi

python - 元素树 - 在不循环的情况下搜索特定元素值

转载 作者:太空宇宙 更新时间:2023-11-03 23:56:32 25 4
gpt4 key购买 nike

我有一个 xml 文件,其中包含一堆重复的 xml block 。仅当报价具有特定的 SellerId 时,我才想提取标价我的商品有 SubCondtion='New' .我现在可以通过遍历优惠并检查 OfferSellerId 来做到这一点在 xml block 中针对我的卖家 ID 并确保 Subcondition = 'New'但我想知道是否有办法获取 ListingPrice使用findfindall检查是否 SellerIdSubCondition在同一<Offer> block 具有这些特定值,仅使用元素树逻辑,而不是对每个报价进行循环。

当前代码

for offer in root.findall('./NotificationPayload/AnyOfferChangedNotification/Offers/'):
OfferSellerId = offer.find('SellerId').text
SubCondition = offer.find('SubCondition').text

if (SellerId == OfferSellerId && SubCondition == 'New'):
ListingPrice = offer.find('ListingPrice/Amount').text

xml

  .......
<Offers>
<Offer>
<SellerId>A2LZYV</SellerId>
<SubCondition>new</SubCondition>
<SellerFeedbackRating>
<SellerPositiveFeedbackRating>100</SellerPositiveFeedbackRating>
<FeedbackCount>929</FeedbackCount>
</SellerFeedbackRating>
<ShippingTime minimumHours="24" maximumHours="48" availabilityType="NOW"/>
<ListingPrice>
<Amount>16.69</Amount>
<CurrencyCode>USD</CurrencyCode>
</ListingPrice>
<Shipping>
<Amount>0.00</Amount>
<CurrencyCode>USD</CurrencyCode>
</Shipping>
<ShipsFrom>
<Country>US</Country>
<State>FL</State>
</ShipsFrom>
<IsFulfilledByAmazon>false</IsFulfilledByAmazon>
<IsBuyBoxWinner>false</IsBuyBoxWinner>
<PrimeInformation>
<IsPrime>false</IsPrime>
<IsNationalPrime>false</IsNationalPrime>
</PrimeInformation>
<IsFeaturedMerchant>true</IsFeaturedMerchant>
<ShipsDomestically>true</ShipsDomestically>
</Offer>
......

最佳答案

尽管ElementTree has limited XPath support ,它应该足以做你想做的事......

XML 输入 (test.xml)

<doc>
<NotificationPayload>
<AnyOfferChangedNotification>
<Offers>
<Offer>
<SellerId>A2LZYV</SellerId>
<SubCondition>new</SubCondition>
<SellerFeedbackRating>
<SellerPositiveFeedbackRating>100</SellerPositiveFeedbackRating>
<FeedbackCount>929</FeedbackCount>
</SellerFeedbackRating>
<ShippingTime minimumHours="24" maximumHours="48" availabilityType="NOW"/>
<ListingPrice>
<Amount>16.69</Amount>
<CurrencyCode>USD</CurrencyCode>
</ListingPrice>
<Shipping>
<Amount>0.00</Amount>
<CurrencyCode>USD</CurrencyCode>
</Shipping>
<ShipsFrom>
<Country>US</Country>
<State>FL</State>
</ShipsFrom>
<IsFulfilledByAmazon>false</IsFulfilledByAmazon>
<IsBuyBoxWinner>false</IsBuyBoxWinner>
<PrimeInformation>
<IsPrime>false</IsPrime>
<IsNationalPrime>false</IsNationalPrime>
</PrimeInformation>
<IsFeaturedMerchant>true</IsFeaturedMerchant>
<ShipsDomestically>true</ShipsDomestically>
</Offer>
</Offers>
</AnyOfferChangedNotification>
</NotificationPayload>
</doc>

python

import xml.etree.ElementTree as ET

root = ET.parse("test.xml")

SellerId = "A2LZYV"

for price in root.findall(f".//Offer[SellerId='{SellerId}'][SubCondition='new']/ListingPrice/Amount"):
print(price.text)

打印输出

16.69

关于python - 元素树 - 在不循环的情况下搜索特定元素值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57533732/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com