如何解析python中的xml?

时间:2017-11-28 23:29:59

标签: python xml xml-parsing

我有这个xml文件:

<?xml version="1.0" encoding="utf-8" ?>
<srl>

<role>V</role><txt>Representava</txt>
<role>A2</role><txt>ela</txt>
<role>A1</role>
<txt>uma jibóia
    <role>A0</role><txt>que</txt>
    <role>V</role><txt>engolia</txt>
    <role>A1</role><txt>uma fera</txt>
</txt>
</srl>

如何在python中提取这个块?我正在使用美丽的汤。

<txt>uma jibóia
    <role>A0</role><txt>que</txt>
    <role>V</role><txt>engolia</txt>
    <role>A1</role><txt>uma fera</txt>
</txt>

我试过了:

soup = bs(open(xml, 'r'), 'lxml')
texts = soup.find_all('txt')

for t in texts:
    print t.text

1 个答案:

答案 0 :(得分:0)

我解决了它:

for t in texts:
    if len(t.contents) > 1:
       print t