0
<manifest>
    <item href="Text/Briefcontent.xhtml" id="Briefcontent.xhtml" media-type="application/xhtml+xml" />
    <item href="Text/Section115.xhtml" id="Section115.xhtml" media-type="application/xhtml+xml" />
 </manifest>

次のような結果が必要です。key=id, value=href

dict={'Briefcontent.xhtml':'Text/Briefcontent.xhtml', 'Section115.xhtml':'Text/Section115.xhtml'}

Pythonコードは何を書けばいいですか?</p>

4

1 に答える 1

3

使用できますBeautifulSoup

>>> from bs4 import BeautifulSoup as BS
>>> xml = """<manifest>
...     <item href="Text/Briefcontent.xhtml" id="Briefcontent.xhtml" media-type="application/xhtml+xml" />
...     <item href="Text/Section115.xhtml" id="Section115.xhtml" media-type="application/xhtml+xml" />
...  </manifest>"""
>>> soup = BS(xml, 'xml')
>>> d = {}
>>> for i in soup.findAll('item'):
...     idict = i.attrs
...     d[idict['id']] = idict['href']
>>> d
{u'Briefcontent.xhtml': u'Text/Briefcontent.xhtml', u'Section115.xhtml': u'Text/Section115.xhtml'}
于 2013-06-04T12:35:33.237 に答える