Home > Article > Backend Development > How to clear content in html file in python
How to clear the content in html files in python: 1. Use the join method, the code is [pat = re.compile('>(.*?)<')''.join(pat.findall (test))]; 2. Use the compile method.
The operating environment of this tutorial: Windows 7 system, python version 3.9, DELL G3 computer. This method is suitable for all brands of computers.
Python method to clear the content in html files:
Method 1:
In [97]: str_ = '' ...: flag = 1 ...: for ele in test: ...: if ele == "<": ...: flag = 0 ...: elif ele == '>': ...: flag = 1 ...: continue ...: if flag == 1: ...: str_ += ele ...: In [98]: str_ Out[98]: 'just for testjust for testtest' In [99]: str_ = '' ...: flag = 1 ...: for ele in test: ...: if ele == "<": ...: flag = 0 ...: elif ele == '>': ...: flag = 1 ...: ele = ' ' ...: if flag == 1: ...: str_ += ele ...: In [100]: str_ Out[100]: ' just for test just for test test '
Method 2:
import re In [156]: pat = re.compile('(?<=\>).*?(?=\<)') In [157]: pat.findall(test) Out[157]: ['just for test', '', '', 'just for test', '', 'test'] In [158]: ''.join(pat.findall(test)) Out[158]: 'just for testjust for testtest'
Method 3:
pat = re.compile('>(.*?)<') ''.join(pat.findall(test))
Method 4:
In [167]: pat = re.compile('<[^>]+>', re.S) In [168]: pat.sub('', test) Out[168]: 'just for testjust for testtest'
A large number of free learning recommendations, please visit python tutorial(video)
The above is the detailed content of How to clear content in html file in python. For more information, please follow other related articles on the PHP Chinese website!