我正在处理一个非常粗糙的html文件。
它看起来像这样:
<p><i><font size="2" style="font-size:10.0pt;font-style:italic;"> The
Company’s future results</font></i><i><font size="2" style="font-size:10.0pt;font-style:italic;">
and energy,</font></i><i><font style="font-style:italic;">including oil
and natural gas</font></i><i><font style="font-style:italic;">are under risk</font></i>Some text in the p tag</p>
文本The Company's future results and energy, including oil and natural gas are under risk位于多个<i>标记中
有没有办法让我在这个文本中只有一个<i>。(我不关心字体标签)。我的html应该是这样的:
<p><i><font size="2" style="font-size:10.0pt;font-style:italic;"> The
Company’s future results</font><font size="2" style="font-size:10.0pt;font-style:italic;">
and energy,</font><font style="font-style:italic;">including oil
and natural gas</font><font style="font-style:italic;">are under risk</font></i>Some text in the p tag</p>
发布于 2019-07-03 13:07:45
您可以简单地使用正则表达式来做到这一点。像这样:
import re
html= re.sub(r'</i><i>', '', html, flags=re.I)当然,如果您确定i-tag始终以小写形式写入,则可以简单地执行以下操作:
html= html.replace('</i><i>', '')这两个版本都依赖于这样的假设,即开头和结尾的i-tag相邻出现,以便被替换(但我猜这就是您想要的,对吧?)
https://stackoverflow.com/questions/56863001
复制相似问题