[英]How to remove unnecessary tags?
I have field "body" in my table (mysql) and there a lot of entries like: 我在我的表(mysql)中有字段“body”,并且有很多条目,例如:
</p><p> </p><p>
</p><p>
</p><p>
A lot of spaces, new line,  , etc. How to remove it? 很多空间,新线,等等。如何删除它?
This not work: 这不起作用:
text.replace('</p><p> </p><p>', '</p><p>')
text.replace('</p><p>\n</p><p>', '</p><p>')
text = ''.join(text.split())
- 之后您可以继续替换。
I would parse such a file in a syntax tree, and then removed there empty leaves. 我会在语法树中解析这样的文件,然后删除空叶。 Then would generate the HTML file again. 然后会再次生成HTML文件。 Unfortunately I'm not working in python, I cannot specify the helpful libraries for this. 不幸的是我不在python中工作,我无法为此指定有用的库。
What @Jurlie Suggested is a Good approach. 什么@Jurlie建议是一个很好的方法。 Consider using BeautifulSoup for this purpouse. 考虑将BeautifulSoup用于此purpouse。 It is a very mature and robust library. 它是一个非常成熟和强大的库。
Try this regexp: 试试这个正则表达式:
>>> import re
>>> text = '''</p><p> </p><p>
...
... </p><p>
... </p><p>
... '''
>>> re.sub(r'<p>(?: |\s|<br \/>)*?</p>\s*', '', text)
'</p><p>\n'
text.strip('> ').strip(' ').strip('\n').strip('\t')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.