如果不存在则使用 getElementsByTagName 跳过元素
Skip element with getElementsByTagName if it doesn't exist
我有一个脚本可以解析 XML 文件以查找某些属性。但是,当我尝试定义一个不存在的属性时,它会抛出一个错误。解决此问题的最佳方法是什么?
例如,此代码查找 API.
给出的所有作品
elif respCode == '4':
works = xdoc.getElementsByTagName('works')[0]
print " " + 'Works found: ' + str(len(works.getElementsByTagName('work'))) + ' different works'
for work in works.getElementsByTagName('work'):
author = work.attributes["author"].value
title = work.attributes["title"].value
editionCount = work.attributes["editions"].value
date = work.attributes["lyr"].value
format = work.attributes["format"].value
owi = work.attributes["owi"].value
wi = work.attributes["wi"].value
当输入以下 XML 文件时
<classify xmlns="http://classify.oclc.org">
<response code="4"/>
<!-- Classify is a product of OCLC Online Computer Library Center: http://classify.oclc.org -->
<workCount>7</workCount>
<start>0</start>
<maxRecs>25</maxRecs>
<orderBy>thold desc</orderBy>
<input type="isbn">1</input>
<works>
<work author="Barlow, Alfred E. (Alfred Ernest), 1861-1914 | Geological Survey of Canada" editions="33" format="eBook" holdings="270" hyr="2018" itemtype="itemtype-book-digital" lyr="1904" owi="12532881" schemes="DDC LCC" title="Reprint of a report on the origin, geological relations and composition of the nickel and copper deposits of the Sudbury Mining District, Ontario, Canada" wi="9090518"/>
<work author="Skillen, James W." editions="2" format="Book" holdings="237" hyr="2014" itemtype="itemtype-book" lyr="2014" owi="1361997817" schemes="DDC LCC" title="The good of politics : a biblical, historical, and contemporary introduction" wi="849787504"/>
<work author="Buchori, Binny | Buchori, Binny [Contributor] | Husain, Thamrin, 1974- | Salampessy, Zairin, 1968-" editions="4" format="Book" holdings="21" hyr="2011" itemtype="itemtype-book" lyr="2001" owi="475047565" schemes="DDC LCC" title="Ketika semerbak cengkih tergusur asap mesiu : tragedi kemanusiaan Maluku di balik konspirasi militer, kapitalis birokrat, dan kepentingan elit politik" wi="48642781"/>
<work author="Bauman, Amy" editions="3" format="Book" holdings="11" hyr="2009" itemtype="itemtype-book" lyr="2009" owi="481071496" schemes="DDC" title="Pirate's treasure : a peek-a-boo adventure" wi="615048025"/>
<work author="Stanton, Geoffrey | CfBT Education Trust" editions="3" format="eBook" holdings="9" hyr="2015" itemtype="itemtype-book-digital" lyr="2008" owi="4889708365" schemes="DDC" title="Learning matters : making the 14-19 reforms work for learners : by emphasising learning programmes as well as qualifications : by learning from previous initiatives" wi="751807280"/>
<work author="Ide, Arthur Frederick" editions="2" format="Book" holdings="5" hyr="1985" itemtype="itemtype-book" lyr="1985" owi="64427876" schemes="DDC LCC" title="Idol worshippers in America : Phyllis Schlafly, Ronald Reagan, Jerry Falwell, and the Moral Majority on women, work, and homosexuality : with a parallel translation and critical commentary on Genesis 19" wi="79169264"/>
<work editions="3" format="Book" holdings="5" hyr="2020" itemtype="itemtype-book" lyr="2020" owi="10209736909" schemes="DDC" title="52 weeks of socks" wi="1142963815"/>
</works>
</classify>
代码在最后一个元素上跳闸,因为未定义标记 <author>
。如果 XML 文件中的标记未定义,我如何将我的 author
变量定义为某个值?
谢谢!
您可以使用 try except
块解决此问题,您的代码将如下所示:
try:
author = work.attributes["author"].value
except:
author = 'defaultValue'
您可以找到有关 try/except 块如何工作的更多信息 here
我有一个脚本可以解析 XML 文件以查找某些属性。但是,当我尝试定义一个不存在的属性时,它会抛出一个错误。解决此问题的最佳方法是什么?
例如,此代码查找 API.
给出的所有作品 elif respCode == '4':
works = xdoc.getElementsByTagName('works')[0]
print " " + 'Works found: ' + str(len(works.getElementsByTagName('work'))) + ' different works'
for work in works.getElementsByTagName('work'):
author = work.attributes["author"].value
title = work.attributes["title"].value
editionCount = work.attributes["editions"].value
date = work.attributes["lyr"].value
format = work.attributes["format"].value
owi = work.attributes["owi"].value
wi = work.attributes["wi"].value
当输入以下 XML 文件时
<classify xmlns="http://classify.oclc.org">
<response code="4"/>
<!-- Classify is a product of OCLC Online Computer Library Center: http://classify.oclc.org -->
<workCount>7</workCount>
<start>0</start>
<maxRecs>25</maxRecs>
<orderBy>thold desc</orderBy>
<input type="isbn">1</input>
<works>
<work author="Barlow, Alfred E. (Alfred Ernest), 1861-1914 | Geological Survey of Canada" editions="33" format="eBook" holdings="270" hyr="2018" itemtype="itemtype-book-digital" lyr="1904" owi="12532881" schemes="DDC LCC" title="Reprint of a report on the origin, geological relations and composition of the nickel and copper deposits of the Sudbury Mining District, Ontario, Canada" wi="9090518"/>
<work author="Skillen, James W." editions="2" format="Book" holdings="237" hyr="2014" itemtype="itemtype-book" lyr="2014" owi="1361997817" schemes="DDC LCC" title="The good of politics : a biblical, historical, and contemporary introduction" wi="849787504"/>
<work author="Buchori, Binny | Buchori, Binny [Contributor] | Husain, Thamrin, 1974- | Salampessy, Zairin, 1968-" editions="4" format="Book" holdings="21" hyr="2011" itemtype="itemtype-book" lyr="2001" owi="475047565" schemes="DDC LCC" title="Ketika semerbak cengkih tergusur asap mesiu : tragedi kemanusiaan Maluku di balik konspirasi militer, kapitalis birokrat, dan kepentingan elit politik" wi="48642781"/>
<work author="Bauman, Amy" editions="3" format="Book" holdings="11" hyr="2009" itemtype="itemtype-book" lyr="2009" owi="481071496" schemes="DDC" title="Pirate's treasure : a peek-a-boo adventure" wi="615048025"/>
<work author="Stanton, Geoffrey | CfBT Education Trust" editions="3" format="eBook" holdings="9" hyr="2015" itemtype="itemtype-book-digital" lyr="2008" owi="4889708365" schemes="DDC" title="Learning matters : making the 14-19 reforms work for learners : by emphasising learning programmes as well as qualifications : by learning from previous initiatives" wi="751807280"/>
<work author="Ide, Arthur Frederick" editions="2" format="Book" holdings="5" hyr="1985" itemtype="itemtype-book" lyr="1985" owi="64427876" schemes="DDC LCC" title="Idol worshippers in America : Phyllis Schlafly, Ronald Reagan, Jerry Falwell, and the Moral Majority on women, work, and homosexuality : with a parallel translation and critical commentary on Genesis 19" wi="79169264"/>
<work editions="3" format="Book" holdings="5" hyr="2020" itemtype="itemtype-book" lyr="2020" owi="10209736909" schemes="DDC" title="52 weeks of socks" wi="1142963815"/>
</works>
</classify>
代码在最后一个元素上跳闸,因为未定义标记 <author>
。如果 XML 文件中的标记未定义,我如何将我的 author
变量定义为某个值?
谢谢!
您可以使用 try except
块解决此问题,您的代码将如下所示:
try:
author = work.attributes["author"].value
except:
author = 'defaultValue'
您可以找到有关 try/except 块如何工作的更多信息 here