[英]Trying to check if a tag exists in XML before parsing
I need to check the existence of certain tags in an XML file before parsing it;在解析之前,我需要检查 XML 文件中是否存在某些标签; I'm using Element Tree in Python.
我在 Python 中使用元素树。 Reading here , I tried writing this:
读到这里,我试着写这个:
tgz_xml = f"https://www.ncbi.nlm.nih.gov/pmc/utils/oa/oa.fcgi?id=PMC8300416"
response = urllib.request.urlopen(tgz_xml).read()
tree = ET.fromstring(response)
for OA in tree.findall('OA'):
records = OA.find('records')
if records is None:
print('records missing')
else:
print('records found')
I need to check if the "records" tag exists.我需要检查“记录”标签是否存在。 I don't get an error, but this doesn't print out anything.
我没有收到错误,但这不会打印出任何东西。 What did I do wrong?
我做错了什么? Thank you!
谢谢!
When parsing this XML document variable tree
already points to element OA
, so when searching for this element expression tree.findall('OA')
returns an empty list and loop isn't executed.解析此 XML 文档变量
tree
时已经指向元素OA
,因此在搜索此元素时,表达式tree.findall('OA')
返回一个空列表并且不执行循环。 Remove that line and code will be executed:删除该行并执行代码:
import xml.etree.ElementTree as ET
from urllib.request import urlopen
tgz_xml = f"https://www.ncbi.nlm.nih.gov/pmc/utils/oa/oa.fcgi?id=PMC8300416"
with urlopen(tgz_xml) as conn:
response = conn.read()
tree = ET.fromstring(response)
records = tree.find('records')
if records is None:
print('records missing')
else:
print('records found')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.