【发布时间】:2021-05-29 14:05:20
【问题描述】:
我在 XML 格式的 <list> 中有大量不同作者及其所选作品的列表(命名为 bibliography.xml)。这是一个例子:
<list type="index">
<item><persName>Poe, Edgar Allan</persName>, <note>1809—1849</note>, <bibl>The Black
Cat 1843 (<abbr>Cat.</abbr>).</bibl> — <bibl>The Gold-Bug 1843
(<abbr>Bug.</abbr>).</bibl> — <bibl>The Raven 1845
(<abbr>Rav.</abbr>).</bibl></item>
<item><persName>Melville, Herman</persName>, <bibl>Benito Cereno 1855
(<abbr>Ben.</abbr>)</bibl> — <bibl>Moby-Dick 1851
(<abbr>MobD.</abbr>)</bibl> — <bibl>Typee: A Peep at Polynesian Life 1846
(<abbr>PolyL.</abbr>)</bibl></item>
<item><persName>Barth, John</persName>, <note>(*1930)</note>, <bibl>The Sot-Weed
Factor 1960 (<abbr>Fac.</abbr>)</bibl> — <bibl>Giles Goat-Boy 1960
(<abbr>Gil.</abbr>)</bibl></item>
</list>
import xml.etree.ElementTree as ET
tree = ET.parse('bibliography.xml')
root = tree.getroot()
for work in root:
if(work.tag=='item'):
print work.get('persName')
if (attr.tag=='abbr')
print (attr.text)
显然它不起作用,但由于我对 python 完全陌生,所以我无法全神贯注于我做错了什么。如果有人可以在这里帮助我,将不胜感激。
【问题讨论】:
-
好吧,这很奇怪,因为 Oxygen 和其他一些验证器可以使用 XML。请记住,我只是发布了
<list>的 sn-p,而不是整个 TEI-Header、正文等。