我如何删除< p> < / p为H.与python子 [英] How can i remove <p> </p> with python sub
本文介绍了我如何删除< p> < / p为H.与python子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
mystring =This< p>< / p>< p>是测试< p>< p>< / p>< p>< / p>
result = mystring.sub(< p>< / p>,& nbsp;)
这是行不通的。
解决方案
请不要尝试使用正则表达式解析HTML。使用适当的解析模块,如 htmlparser
或 BeautifulSoup
来达到此目的。 受苦了一段简短的学习曲线,并从中获益:
- 您的解析代码将更加健壮,处理您可能没有考虑过的角落案例使用正则表达式会失败
- 对于未来的HTML解析/自动化任务,您将被授权更快地完成任务,因此最终时间投资也会得到回报。
您不会感到抱歉!保证利润!
I have an html file and I want to replace the empty paragraphs with a space.
mystring = "This <p></p><p>is a test</p><p></p><p></p>"
result = mystring.sub("<p></p>" , " ")
This is not working.
解决方案
Please, don't try to parse HTML with regular expressions. Use a proper parsing module, like htmlparser
or BeautifulSoup
to achieve this. "Suffer" a short learning curve now and benefit:
- Your parsing code will be more robust, handling corner cases you may not have considered that will fail with a regex
- For future HTML parsing/munging tasks, you will be empowered to do things faster, so eventually the time investment pays off as well.
You won't be sorry! Profit guaranteed!
这篇关于我如何删除< p> < / p为H.与python子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文