gpt4 book ai didi

python - 关于正则表达式和 XML

转载 作者:数据小太阳 更新时间:2023-10-29 02:48:12 25 4
gpt4 key购买 nike

我有 XML 格式的数据。示例如下所示。我想从 <text> tag 中提取数据.这是我的 XML 数据。

    <text>
The 40-Year-Old Virgin is a 2005 American buddy comedy
film about a middle-aged man's journey to finally have sex.

<h1>The plot</h1>
Andy Stitzer (Steve Carell) is the eponymous 40-year-old virgin.
<h1>Cast</h1>

<h1>Soundtrack</h1>

<h1>External Links</h1>
</text>

我只需要 The 40-Year-Old Virgin is a 2005 American buddy comedy film about a middle-aged man's journey to finally have sex.是否可以?谢谢

最佳答案

使用 XML 解析器来解析 XML。使用lxml :

import lxml.etree as ET

content='''\
<text>
The 40-Year-Old Virgin is a 2005 American buddy comedy
film about a middle-aged man's journey to finally have sex.

<h1>The plot</h1>
Andy Stitzer (Steve Carell) is the eponymous 40-year-old virgin.
<h1>Cast</h1>

<h1>Soundtrack</h1>

<h1>External Links</h1>
</text>
'''

text=ET.fromstring(content)
print(text.text)

产量

    The 40-Year-Old Virgin is a 2005 American buddy comedy
film about a middle-aged man's journey to finally have sex.

关于python - 关于正则表达式和 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7838290/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com