gpt4 book ai didi

xml - python etree.ElementTree 解析困难的命名空间

转载 作者:行者123 更新时间:2023-12-04 16:57:16 26 4
gpt4 key购买 nike

我实际上必须解析一个 online resource但试图通过将 .xml 文件复制到我的货币 .py 文件所在的目录来简化它。然后从 manual here 开始对于我眼中的复杂事物来说,这很短。
在此示例中,我将文件缩减为 3 个项目和 2 个货币,并为简单起见将其解析为字符串。

import xml.etree.ElementTree as ET
#tree = ET.ElementTree(file='currency.xml')
data = '''
<rdf:RDF xmlns:cb="http://www.cbwiki.net/wiki/index.php/Specification_1.2/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/">
<channel rdf:about="https://www.snb.ch/de/ifor/media/id/media_rss">
<title>SNB Devisenkurse</title>
<link>https://www.snb.ch/de/ifor/media/id/media_rss</link>
<description>Schweizerische Nationalbank (SNB): Devisenkurse (Ankauf Zürich 11 Uhr)</description>
<items>
<rdf:Seq>
<rdf:li rdf:resource="https://www.snb.ch#GBP_4782176f7871403c002581de004629c7"/>
<rdf:li rdf:resource="https://www.snb.ch#JPY_4782176f7871403c002581de004629c7"/>
<rdf:li rdf:resource="https://www.snb.ch#EUR_4782176f7871403c002581de004629c7"/>
<!-- here are more currencys... -->
</rdf:Seq>
</items>
<dc:publisher>SNB</dc:publisher>
<dc:rights>Copyright © Schweizerische Nationalbank, Zürich (Schweiz) 2017</dc:rights>
<dcterms:license>https://www.snb.ch/de/srv/id/disclaimer</dcterms:license>
<dcterms:created>2017-11-20T19:30:01+01:00</dcterms:created>
</channel>
<item rdf:about="https://www.snb.ch#GBP_4782176f7871403c002581de004629c7">
<title>CH: 1.3116 CHF = 1 GBP 2017-11-20 Tägliche Kurse (11:00)</title>
<link>https://www.snb.ch</link>
<description>1 GBP = 1.3116 CHF (Tägliche Kurse (11:00); 2017-11-20T13:46:22.357+01:00)</description>
<dc:date>2017-11-20T13:46:22.357+01:00</dc:date>
<dc:language>de</dc:language>
<cb:statistics rdf:parseType="Resource">
<rdf:type rdf:resource="http://www.cbwiki.net/wiki/index.php/RSS-CB_1.2_RDF_Schema#Statistics"/>
<cb:country>CH</cb:country>
<cb:institutionAbbrev>SNB</cb:institutionAbbrev>
<cb:exchangeRate rdf:parseType="Resource">
<rdf:type rdf:resource="http://www.cbwiki.net/wiki/index.php/RSS-CB_1.2_RDF_Schema#ExchangeRate"/>
<cb:observation rdf:parseType="Resource">
<rdf:type rdf:resource="http://www.cbwiki.net/wiki/index.php/RSS-CB_1.2_RDF_Schema#Observation"/>
<cb:value>1.3116</cb:value>
<cb:unit>CHF</cb:unit>
<cb:decimals>4</cb:decimals>
</cb:observation>
<cb:baseCurrency>CHF</cb:baseCurrency>
<cb:targetCurrency>GBP</cb:targetCurrency>
<cb:rateType>Tägliche Kurse (11:00)</cb:rateType>
<cb:observationPeriod rdf:parseType="Resource">
<rdf:type rdf:resource="http://www.cbwiki.net/wiki/index.php/RSS-CB_1.2_RDF_Schema#ObservationPeriod"/>
<cb:frequency>daily</cb:frequency>
<cb:period>2017-11-20</cb:period>
</cb:observationPeriod>
</cb:exchangeRate>
</cb:statistics>
</item>
<item rdf:about="https://www.snb.ch#JPY_4782176f7871403c002581de004629c7">
<title>CH: 0.8819 CHF = 100 JPY 2017-11-20 Tägliche Kurse (11:00)</title>
<link>https://www.snb.ch</link>
<description>100 JPY = 0.8819 CHF (Tägliche Kurse (11:00); 2017-11-20T13:46:22.350+01:00)</description>
<dc:date>2017-11-20T13:46:22.350+01:00</dc:date>
<dc:language>de</dc:language>
<cb:statistics rdf:parseType="Resource">
<rdf:type rdf:resource="http://www.cbwiki.net/wiki/index.php/RSS-CB_1.2_RDF_Schema#Statistics"/>
<cb:country>CH</cb:country>
<cb:institutionAbbrev>SNB</cb:institutionAbbrev>
<cb:exchangeRate rdf:parseType="Resource">
<rdf:type rdf:resource="http://www.cbwiki.net/wiki/index.php/RSS-CB_1.2_RDF_Schema#ExchangeRate"/>
<cb:observation rdf:parseType="Resource">
<rdf:type rdf:resource="http://www.cbwiki.net/wiki/index.php/RSS-CB_1.2_RDF_Schema#Observation"/>
<cb:value>0.8819</cb:value>
<cb:unit>CHF</cb:unit>
<cb:unit_mult>-2</cb:unit_mult>
<cb:decimals>4</cb:decimals>
</cb:observation>
<cb:baseCurrency>CHF</cb:baseCurrency>
<cb:targetCurrency>JPY</cb:targetCurrency>
<cb:rateType>Tägliche Kurse (11:00)</cb:rateType>
<cb:observationPeriod rdf:parseType="Resource">
<rdf:type rdf:resource="http://www.cbwiki.net/wiki/index.php/RSS-CB_1.2_RDF_Schema#ObservationPeriod"/>
<cb:frequency>daily</cb:frequency>
<cb:period>2017-11-20</cb:period>
</cb:observationPeriod>
</cb:exchangeRate>
</cb:statistics>
</item>
<item rdf:about="https://www.snb.ch#EUR_4782176f7871403c002581de004629c7">
<title>CH: 1.1665 CHF = 1 EUR 2017-11-20 Tägliche Kurse (11:00)</title>
<link>https://www.snb.ch</link>
<description>1 EUR = 1.1665 CHF (Tägliche Kurse (11:00); 2017-11-20T13:46:22.343+01:00)</description>
<dc:date>2017-11-20T13:46:22.343+01:00</dc:date>
<dc:language>de</dc:language>
<cb:statistics rdf:parseType="Resource">
<rdf:type rdf:resource="http://www.cbwiki.net/wiki/index.php/RSS-CB_1.2_RDF_Schema#Statistics"/>
<cb:country>CH</cb:country>
<cb:institutionAbbrev>SNB</cb:institutionAbbrev>
<cb:exchangeRate rdf:parseType="Resource">
<rdf:type rdf:resource="http://www.cbwiki.net/wiki/index.php/RSS-CB_1.2_RDF_Schema#ExchangeRate"/>
<cb:observation rdf:parseType="Resource">
<rdf:type rdf:resource="http://www.cbwiki.net/wiki/index.php/RSS-CB_1.2_RDF_Schema#Observation"/>
<cb:value>1.1665</cb:value>
<cb:unit>CHF</cb:unit>
<cb:decimals>4</cb:decimals>
</cb:observation>
<cb:baseCurrency>CHF</cb:baseCurrency>
<cb:targetCurrency>EUR</cb:targetCurrency>
<cb:rateType>Tägliche Kurse (11:00)</cb:rateType>
<cb:observationPeriod rdf:parseType="Resource">
<rdf:type rdf:resource="http://www.cbwiki.net/wiki/index.php/RSS-CB_1.2_RDF_Schema#ObservationPeriod"/>
<cb:frequency>daily</cb:frequency>
<cb:period>2017-11-20</cb:period>
</cb:observationPeriod>
</cb:exchangeRate>
</cb:statistics>
</item>
<!-- here are more currencys... -->
</rdf:RDF>'''
tree = ET.fromstring(data)
ns = {'rdf':'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
'cb': 'http://www.cbwiki.net/wiki/index.php/Specification_1.2/'}
for item in tree.findall('item'):
for stat in item.findall('cb:statistics', ns):
for exrate in stat.findall('cb:exchangeRate', ns):
currency = exrate.find('cb:targetCurrency', ns).text
print(currency)

所以,那只是一个 MWE。我实际上做的是最后,只有几行:
tree = ET.fromstring(data)
ns = {'rdf':'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
'cb': 'http://www.cbwiki.net/wiki/index.php/Specification_1.2/'}
for item in tree.findall('item'):
for stat in item.findall('cb:statistics', ns):
for exrate in stat.findall('cb:exchangeRate', ns):
currency = exrate.find('cb:targetCurrency', ns).text
print(currency)

现在去终端并执行:
python3货币.py
没发生什么事...

我想获得日元的货币 (cb:targetCurrency) 并将其乘以 10 x (cb:unit_mult) 字段。但是现在我无法获得节点的任何 .text ......
我怎样才能获得货币?

解决方案:
import xml.etree.ElementTree as ET
# terminal: cd to this .py file and then do
# $wget https://www.snb.ch/selector/de/mmr/exfeed/rss
# rename the file to rss.xml
tree = ET.ElementTree(file='rss.xml')
root = tree.getroot()
ns = {'rdf':'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
'none':'http://purl.org/rss/1.0/',
'dc':'http://purl.org/dc/elements/1.1/',
'dcterms':'http://purl.org/dc/terms/',
'cb': 'http://www.cbwiki.net/wiki/index.php/Specification_1.2/'}

for item in root.findall('none:item',ns):
for statistics in item.findall('cb:statistics',ns):
for exchangerate in statistics.findall('cb:exchangeRate',ns):
for targetcurrency in exchangerate.findall('cb:targetCurrency',ns):
print(targetcurrency.text)

最佳答案

您没有考虑默认命名空间。没有前缀的后代元素隐式继承了这个命名空间,即包括 item您尝试迭代的元素:

ns = {'d': 'http://purl.org/rss/1.0/',
....}
for item in tree.findall('d:item', ns):
.....

关于xml - python etree.ElementTree 解析困难的命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47491313/

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