当我传递特定参数时,使用ElementTree的iter()解析XML找不到我的标签
试图返回属性&标记中的值.按照ElementTree文档逐字逐句地进行任何操作都不会产生任何效果.没有错误,它只是运行并且不打印任何内容.如果我不带参数运行iter(),它将打印每个标签,但是带参数则不执行任何操作.不知道发生了什么.findall()也不起作用.
Trying to return attribute & values from a tag. Following the ElementTree documentation to word for word is producing nothing. No errors, it just runs and doesn't print anything. If I run iter() with no arguments it prints every single tag, but with an argument it does nothing. Not sure what's going on. The findall() doesn't work either.
如果我使用文档的XML,它可以正常工作,但不能与我的XML一起工作.我所看到的唯一区别是,文档xml的标签位于同一括号内.
If I use the doc's XML it works fine, but not with mine. Only difference I see are tags closed in the same bracket for the doc XML.
毫无疑问,我正在使用正确的Python版本,所以我很茫然.下面是我首先使用的XML,第二是Doc XML,以及运行它的代码.
I'm using the right version of Python without a doubt, so i'm at a loss. Below is my XML first, Doc XML 2nd, and the code to run it.
<?xml version="1.0" encoding="iso_8859-1"?>
<day xmlns="x-schema:..\schema_ej.xml" FILE="90301007.009">
<trs F1068="SALE" F254="2019-03-01" F253="2019-03-01T12:21:30" F1056="007" F1057="009" F1035="11:52:53" F1036="12:21:30"
F1032="74925" F1764="00074732" F1185="7110" F1126="7110" F1127="Eva S.">
<r F1101="1"><itm F01="0071834383234" F02="SI SSND SOUL 35Z" F04="30" F03="100" F81="1" F79="1" F1007="3.99" F1006="1" F1080="0.938"/><F65>3.99</F65><F64>1</F64><F1263>0.09</F1263><key in="1013" fn="10725"/><key in="1013" fn="10735"/><key in="1013" fn="10746"/><key in="1013" fn="10715"/><key in="1013" fn="10777"/><key in="1013" fn="10736"/><key in="1013" fn="10710"/><key in="1013" fn="10747"/><key in="1013" fn="10775"/><key in="1013" fn="10726"/><key in="1013" fn="10760"/><key in="1013" fn="10200"/><key fn="30"/><key in="71834383234" fn="710"/></r>
</trs>
</day>
<?xml version="1.0"?>
<data>
<country name="Liechtenstein">
<rank>1</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E"/>
<neighbor name="Switzerland" direction="W"/>
</country>
<country name="Singapore">
<rank>4</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<neighbor name="Malaysia" direction="N"/>
</country>
<country name="Panama">
<rank>68</rank>
<year>2011</year>
<gdppc>13600</gdppc>
<neighbor name="Costa Rica" direction="W"/>
<neighbor name="Colombia" direction="E"/>
</country>
</data>
import xml.etree.ElementTree as ET
tree = ET.parse('90301007.xml')
root = tree.getroot()
for trs in root.iter('trs'):
print(trs.attrib)
您的第一个XML的默认命名空间为 x-schema:.. \ schema_ej.xml
.
Your first XML has a default namespace of x-schema:..\schema_ej.xml
.
尝试将您的 iter()
更改为此:
root.iter(r'{x-schema:..\schema_ej.xml}trs')
在此处查看有关ElementTree中名称空间的更多信息.
See here for more info on namespaces in ElementTree.
请参阅此处,以获取有关XML名称空间的一般信息.
See here for more info on XML namespaces in general.