Blu*_*Ice 2 python xml-parsing
我想解析xml文件中的文本.考虑到我在file.xml中有一些行
<s id="1792387-2">Castro Verde is situated in the Baixo Alentejo Subregion within a territory known locally as the Campo Branco (English: White Plains).</s>
Run Code Online (Sandbox Code Playgroud)
如何从上面的行中提取以下文本:
Castro Verde is situated in the Baixo Alentejo Subregion within a territory known locally as the Campo Branco (English: White Plains).
Run Code Online (Sandbox Code Playgroud)
在对文本进行一些更改后,我希望返回带有相同标记的更改文本,如下所示.
<s id="1792387-2"> Changed Text </s>
Run Code Online (Sandbox Code Playgroud)
请任何建议.谢谢!
LXML使这一点变得特别容易.
>>> from lxml import etree
>>> text = '''<s id="1792387-2">Castro Verde is situated in the Baixo Alentejo Subregion within a territory known locally as the Campo Branco (English: White Plains).</s>'''
>>> def edit(s):
... return 'Changed Text'
...
>>> t = etree.fromstring(text)
>>> t.text = edit(t.text)
>>> etree.tostring(t)
'<s id="1792387-2">Changed Text</s>'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1527 次 |
| 最近记录: |