python 2.7:不同的属性
我刚刚开始使用(学习)Python 2.7。我目前关注的是从XML文件中提取信息。到目前为止,xml.etree.ElementTree让我走得相当远。我现在陷入了一个“钥匙错误”。据我所知,原因是元素具有不同的属性 XML文件(更大)的关键部分:python 2.7:不同的属性,python,xml,python-2.7,Python,Xml,Python 2.7,我刚刚开始使用(学习)Python 2.7。我目前关注的是从XML文件中提取信息。到目前为止,xml.etree.ElementTree让我走得相当远。我现在陷入了一个“钥匙错误”。据我所知,原因是元素具有不同的属性 XML文件(更大)的关键部分: <?xml version='1.0' encoding='utf-8' ?> <XMLFILE> <datasources> <datasource caption='Sheet1 (Exce
<?xml version='1.0' encoding='utf-8' ?>
<XMLFILE>
<datasources>
<datasource caption='Sheet1 (ExcelSample)'>
<connection class='excel-direct' filename='~\SomeExcel.xlsx' .....>
......
</connection>
<column header='Unit Price' datatype='real' name='[Calculation_1]' role='measure' type='quantitative'>
<calculation class='calculation' formula='Sum(Profit)/Sum(Sales)' />
</column>
<column datatype='integer' name='[Sales]' role='measure' type='quantitative' user:auto-column='numrec'>
<calculation class='trial' formula='1' />
</column>
</datasource>
</datasources>
........
</XMLFILE>
结果:
Column name: Calculation_1, datatype:real
Column name: Sales, datatype:integer
但是,如果我使用cal.attrib['header']python2.7。印刷品
"KeyError: 'header'
问题:如何告诉Python 2.7。要产生所需的输出:
Calculation "Unit Price": Sum(Profit)/Sum(Sales)
更确切地说,Python应该做什么:“对于包含属性“header”的所有列(=如果有多个类似于上例的列),打印输出
header: Unit Price
formula: Sum(Profit)
header: Sales per day in month
formula: Sales / count(days(month))
(注意:为了显示更完整的所需输出,我添加了另一列,这在我的示例中还没有出现)
非常感谢您的帮助!也许您可以使用“BeautifullSoup”(bs4)模块而不是“xml.etree”
看一看,也许您可以使用“BeautifullSoup”(bs4)模块而不是“xml.etree”
查看并您可以使用XPath谓词表达式根据特定条件筛选元素,即筛选列
元素,该元素具有标题
属性:列[@header]
*。因此您的for
循环如下所示:
for cal in xmlfile.findall('datasources/datasource/column[@header]'):
print "header: " + cal.attrib["header"]
print " formula: " + cal.find('calculation').attrib["formula"]
if "header" in cal.attrib:
print "header: " + cal.attrib["header"]
*)请注意,@attribute\u name
语法用于引用XPath中的XML属性
相反,如果您想迭代所有
列
,而不管它是否具有标题
属性,而只在列
具有属性时打印标题属性值,则可以使用简单的if
块来实现,如下所示:
for cal in xmlfile.findall('datasources/datasource/column[@header]'):
print "header: " + cal.attrib["header"]
print " formula: " + cal.find('calculation').attrib["formula"]
if "header" in cal.attrib:
print "header: " + cal.attrib["header"]
您可以使用XPath谓词表达式按特定条件筛选元素,即筛选列
元素,该元素具有标题
属性:列[@header]
*。因此您的for
循环如下所示:
for cal in xmlfile.findall('datasources/datasource/column[@header]'):
print "header: " + cal.attrib["header"]
print " formula: " + cal.find('calculation').attrib["formula"]
if "header" in cal.attrib:
print "header: " + cal.attrib["header"]
*)请注意,@attribute\u name
语法用于引用XPath中的XML属性
相反,如果您想迭代所有
列
,而不管它是否具有标题
属性,而只在列
具有属性时打印标题属性值,则可以使用简单的if
块来实现,如下所示:
for cal in xmlfile.findall('datasources/datasource/column[@header]'):
print "header: " + cal.attrib["header"]
print " formula: " + cal.find('calculation').attrib["formula"]
if "header" in cal.attrib:
print "header: " + cal.attrib["header"]
KeyError是Python告诉您在该元素中找不到您请求的属性的方式。这没关系,可能是findall xpath引入了一些没有“header”属性的元素。因为您只对那些属于搜索范围且恰好有“header”属性的元素感兴趣属性,您可以执行以下操作:
for cal in xmlfile.findall('datasources/datasource/column'):
try:
header = cal.attrib["header"]
#Do something with the header
print header
except KeyError:
#This is where you end up if the element doesn't have a 'header' attribute
#You shouldn't have to do anything with this 'cal' element
当然,您可以先检查头是否存在,但在使用python一段时间后,我认为这种方法更简单。KeyError是python告诉您在该元素中找不到您请求的属性的方式。这没关系,可能是findall xpath引入了一些没有“header”属性的元素。由于您只对属于您搜索范围的内容感兴趣,并且碰巧附加了“header”属性,因此可以执行以下操作:
for cal in xmlfile.findall('datasources/datasource/column'):
try:
header = cal.attrib["header"]
#Do something with the header
print header
except KeyError:
#This is where you end up if the element doesn't have a 'header' attribute
#You shouldn't have to do anything with this 'cal' element
当然,您可以先检查头部是否存在,但在使用python一段时间后,我认为这种方法更简单。您好,非常感谢。虽然python现在不再给出错误,但不幸的是,它再也不会打印任何内容了……您好,非常感谢。虽然python现在不再给出错误,但不幸的是,它不再打印任何内容。。。。