作为if
语句的一部分,可以测试value
标记属性的XML
吗?你知道吗
例如,我有一个带有以下标记的XML
:
<Cell ss:StyleID="HeadTableTitle" ss:MergeAcross="1"><Data ss:Type="String">Administrative Data</Data></Cell>
我试图测试属性ss:StyleID
==HeadTableTitle
在<Cell>
标记中
from lxml import etree
f_path = 'data store/cortex_full.xml' # enter path of xml file
epoch = 0 # set epoch window size (seconds) for smoothing data (set to 0 to use raw)
with open(f_path, 'r', encoding='utf-8') as f:
root = etree.parse(f)
namespaces = {'o': 'urn:schemas-microsoft-com:office:office',
'x': 'urn:schemas-microsoft-com:office:excel',
'ss': 'urn:schemas-microsoft-com:office:spreadsheet'}
ws = root.xpath('/ss:Workbook/ss:Worksheet', namespaces=namespaces)
if len(ws) > 0:
tables = ws[0].xpath('./ss:Table', namespaces=namespaces)
if len(tables) > 0:
rows = tables[0].xpath('./ss:Row', namespaces=namespaces)
for row in rows:
cells = row.xpath('./ss:Cell', namespaces=namespaces)
if len(cells) == 1: # skip if row have multiple 'cells'
if cells[0].attrib('./@ss:StyleID', namespaces=namespaces) == 'HeadTableTitle':
## execute some code ##
但是,当我执行代码中的最后一行时,它抛出一个TypeError: 'lxml.etree._Attrib' object is not callable
要获取属性的值,可以使用
element.attrib['key']
或element.get('key')
。你知道吗在这两种情况下,如果属性绑定到命名空间,则需要使用完整的命名空间URI。你知道吗
相关问题 更多 >
编程相关推荐