xml extract currency - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: xml extract currency (/thread-37084.html) |
xml extract currency - 3lnyn0 - Apr-30-2022 Hi! I have the xml below, how can I extract the currency who have this value multiplier="100"? <?xml version="1.0" encoding="utf-8"?> <DataSet xmlns="http://www.bnr.ro/xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.bnr.ro/xsd nbrfxrates.xsd"> <Header> <Publisher>National Bank of Romania</Publisher> <PublishingDate>2022-04-29</PublishingDate> <MessageType>DR</MessageType> </Header> <Body> <Subject>Reference rates</Subject> <OrigCurrency>RON</OrigCurrency> <Cube date="2022-04-29"> <Rate currency="AED">1.2734</Rate> <Rate currency="AUD">3.3490</Rate> <Rate currency="BGN">2.5298</Rate> <Rate currency="BRL">0.9470</Rate> <Rate currency="CAD">3.6702</Rate> <Rate currency="CHF">4.8290</Rate> <Rate currency="CNY">0.7096</Rate> <Rate currency="CZK">0.2016</Rate> <Rate currency="DKK">0.6649</Rate> <Rate currency="EGP">0.2530</Rate> <Rate currency="EUR">4.9480</Rate> <Rate currency="GBP">5.8793</Rate> <Rate currency="HRK">0.6538</Rate> <Rate currency="HUF" multiplier="100">1.3122</Rate> <Rate currency="INR">0.0612</Rate> <Rate currency="JPY" multiplier="100">3.5869</Rate> <Rate currency="KRW" multiplier="100">0.3716</Rate> <Rate currency="MDL">0.2543</Rate> <Rate currency="MXN">0.2308</Rate> <Rate currency="NOK">0.5034</Rate> <Rate currency="NZD">3.0529</Rate> <Rate currency="PLN">1.0602</Rate> <Rate currency="RSD">0.0420</Rate> <Rate currency="RUB">0.0657</Rate> <Rate currency="SEK">0.4790</Rate> <Rate currency="THB">0.1365</Rate> <Rate currency="TRY">0.3154</Rate> <Rate currency="UAH">0.1547</Rate> <Rate currency="USD">4.6774</Rate> <Rate currency="XAU">287.9962</Rate> <Rate currency="XDR">6.2922</Rate> <Rate currency="ZAR">0.2941</Rate> </Cube> </Body> </DataSet> RE: xml extract currency - ndc85430 - Apr-30-2022 Have you looked at ElementTree? The standard way to navigate XML is XPath and it has support for that. RE: xml extract currency - 3lnyn0 - Apr-30-2022 (Apr-30-2022, 07:32 AM)ndc85430 Wrote: Have you looked at ElementTree? The standard way to navigate XML is XPath and it has support for that. I looked but I couldn't find anything that could help me RE: xml extract currency - ndc85430 - Apr-30-2022 Really? I find that hard to believe. You just need to go through the Rate elements and find ones that have an attribute called multiplier . Can you do that? The docs show you examples of the kind of thing you can do, so you do need to do some reading and experimenting.Having said that, XPath would still be the better way to do it - again, see the docs for examples. RE: xml extract currency - 3lnyn0 - Apr-30-2022 (Apr-30-2022, 07:54 AM)ndc85430 Wrote: Really? I find that hard to believe. You just need to go through the i resolved it for x in root[1][2]: for key in x.attrib.keys(): if key == 'multiplier': print(x.attrib['currency'])
RE: xml extract currency - snippsat - Apr-30-2022 For me the standard way(for a long time) is to use BS(with lxml as parser for speed) or lxml alone. Of course if new to this then may use stuff from standard library first,just to show that there other option for this. from bs4 import BeautifulSoup soup = BeautifulSoup(xml_data, 'lxml-xml') multi = soup.find_all('Rate', {'multiplier': True}) for item in multi: print(item.attrs.get('currency'))
|