python elementTree 获取以结尾的属性
python elementTree get attribute that ends with
将以下 xml 作为 elementTree 的输入(使用 python 2.7):
<body>
<div region="imageRegion" xml:id="img_SUB6756004155_0" ttm:role="caption" smpte:backgroundImage="#SUB6756004155_0">
</body>
我得到这个属性:
所以我需要找到以 'backgroundImage' 或 'id'
结尾的属性
通常我会这样做:
div.get('region')
但是这里我只知道部分属性名,
是否可以使用正则表达式?
下面的代码片段演示了如何从格式正确的 XML 文档中获取 smpte:backgroundImage
属性的值(问题中的输入文档格式不正确)。
smpte:
表示该属性绑定了一个命名空间,从截图来看是http://smpte-ra.org/schemas/2052-1/2013/smpte-tt
。请注意,ttm
和 smpte
前缀都必须在 XML 文档中声明(xmlns:ttm="..."
和 xmlns:smpte="..."
)。
在get()
调用中,属性名必须在"Clark notation"中给出:{http://smpte-ra.org/schemas/2052-1/2013/smpte-tt}backgroundImage
.
from xml.etree import ElementTree as ET
XML = '''
<body xmlns:ttm="http://www.w3.org/ns/ttml#metadata"
xmlns:smpte="http://smpte-ra.org/schemas/2052-1/2013/smpte-tt">
<div region="imageRegion" xml:id="img_SUB6756004155_0"
ttm:role="caption" smpte:backgroundImage="#SUB6756004155_0"></div>
</body>'''
root = ET.fromstring(XML)
div = root.find("div")
print(div.get("{http://smpte-ra.org/schemas/2052-1/2013/smpte-tt}backgroundImage"))
输出:
#SUB6756004155_0
另一种选择是迭代属性和 return 具有以 backgroundImage
.
结尾的本地名称的属性值
示例...
from xml.etree import ElementTree as ET
XML = '''
<body xmlns:ttm="http://www.w3.org/ns/ttml#metadata"
xmlns:smpte="http://smpte-ra.org/schemas/2052-1/2013/smpte-tt">
<div region="imageRegion" xml:id="img_SUB6756004155_0"
ttm:role="caption" smpte:backgroundImage="#SUB6756004155_0"></div>
</body>'''
root = ET.fromstring(XML)
div = root.find("div")
val = next((v for k, v in div.attrib.items() if k.endswith('backgroundImage')), None)
if val:
print(f"Value: {val}")
输出...
Value: #SUB6756004155_0
虽然这可能很脆弱。它只是 return 找到的第一个属性。
如果这是个问题,也许可以改用列表:
val = [v for k, v in div.attrib.items() if k.endswith('backgroundImage')]
它也会错误地 return 以 "backgroundImage" 结尾的属性(如 "invalid_backgroundImage")。
如果这是个问题,可以改用正则表达式:
val = next((v for k, v in div.attrib.items() if re.match(r".*}backgroundImage$", "}" + k)), None)
如果您能够切换到 lxml,则可以在 xpath 中完成本地名称的测试...
val = div.xpath("@*[local-name()='backgroundImage']")
这个解决方案也适用于我:
r = re.compile(r'img_.+')
image_id = filter(r.match, div.attrib.values())
id = image_id[0].split('_', 1)[1]
id ='SUB6756004155_0'
将以下 xml 作为 elementTree 的输入(使用 python 2.7):
<body>
<div region="imageRegion" xml:id="img_SUB6756004155_0" ttm:role="caption" smpte:backgroundImage="#SUB6756004155_0">
</body>
我得到这个属性:
所以我需要找到以 'backgroundImage' 或 'id'
结尾的属性通常我会这样做:
div.get('region')
但是这里我只知道部分属性名,
是否可以使用正则表达式?
下面的代码片段演示了如何从格式正确的 XML 文档中获取 smpte:backgroundImage
属性的值(问题中的输入文档格式不正确)。
smpte:
表示该属性绑定了一个命名空间,从截图来看是http://smpte-ra.org/schemas/2052-1/2013/smpte-tt
。请注意,ttm
和 smpte
前缀都必须在 XML 文档中声明(xmlns:ttm="..."
和 xmlns:smpte="..."
)。
在get()
调用中,属性名必须在"Clark notation"中给出:{http://smpte-ra.org/schemas/2052-1/2013/smpte-tt}backgroundImage
.
from xml.etree import ElementTree as ET
XML = '''
<body xmlns:ttm="http://www.w3.org/ns/ttml#metadata"
xmlns:smpte="http://smpte-ra.org/schemas/2052-1/2013/smpte-tt">
<div region="imageRegion" xml:id="img_SUB6756004155_0"
ttm:role="caption" smpte:backgroundImage="#SUB6756004155_0"></div>
</body>'''
root = ET.fromstring(XML)
div = root.find("div")
print(div.get("{http://smpte-ra.org/schemas/2052-1/2013/smpte-tt}backgroundImage"))
输出:
#SUB6756004155_0
另一种选择是迭代属性和 return 具有以 backgroundImage
.
示例...
from xml.etree import ElementTree as ET
XML = '''
<body xmlns:ttm="http://www.w3.org/ns/ttml#metadata"
xmlns:smpte="http://smpte-ra.org/schemas/2052-1/2013/smpte-tt">
<div region="imageRegion" xml:id="img_SUB6756004155_0"
ttm:role="caption" smpte:backgroundImage="#SUB6756004155_0"></div>
</body>'''
root = ET.fromstring(XML)
div = root.find("div")
val = next((v for k, v in div.attrib.items() if k.endswith('backgroundImage')), None)
if val:
print(f"Value: {val}")
输出...
Value: #SUB6756004155_0
虽然这可能很脆弱。它只是 return 找到的第一个属性。
如果这是个问题,也许可以改用列表:
val = [v for k, v in div.attrib.items() if k.endswith('backgroundImage')]
它也会错误地 return 以 "backgroundImage" 结尾的属性(如 "invalid_backgroundImage")。
如果这是个问题,可以改用正则表达式:
val = next((v for k, v in div.attrib.items() if re.match(r".*}backgroundImage$", "}" + k)), None)
如果您能够切换到 lxml,则可以在 xpath 中完成本地名称的测试...
val = div.xpath("@*[local-name()='backgroundImage']")
这个解决方案也适用于我:
r = re.compile(r'img_.+')
image_id = filter(r.match, div.attrib.values())
id = image_id[0].split('_', 1)[1]
id ='SUB6756004155_0'