Home > Backend Development > Python Tutorial > How to clear content in html file in python

How to clear content in html file in python

coldplay.xixi
Release: 2023-01-03 09:24:03
Original
2898 people have browsed it

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.

How to clear content in html file in python

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_ = &#39;&#39;
    ...: flag = 1
    ...: for ele in test:
    ...:     if ele == "<":
    ...:         flag = 0
    ...:     elif ele == &#39;>&#39;:
    ...:         flag = 1
    ...:         continue
    ...:     if flag == 1:
    ...:         str_ += ele
    ...:         
In [98]: str_
Out[98]: &#39;just for testjust for testtest&#39;
In [99]: str_ = &#39;&#39;
    ...: flag = 1
    ...: for ele in test:
    ...:     if ele == "<":
    ...:         flag = 0
    ...:     elif ele == &#39;>&#39;:
    ...:         flag = 1
    ...:         ele = &#39; &#39;
    ...:     if flag == 1:
    ...:         str_ += ele
    ...:         
In [100]: str_
Out[100]: &#39; just for test   just for test  test &#39;
Copy after login

Method 2:

import re
In [156]: pat = re.compile(&#39;(?<=\>).*?(?=\<)&#39;)
In [157]: pat.findall(test)
Out[157]: [&#39;just for test&#39;, &#39;&#39;, &#39;&#39;, &#39;just for test&#39;, &#39;&#39;, &#39;test&#39;]
In [158]: &#39;&#39;.join(pat.findall(test))
Out[158]: &#39;just for testjust for testtest&#39;
Copy after login

Method 3:

pat = re.compile(&#39;>(.*?)<&#39;)
&#39;&#39;.join(pat.findall(test))
Copy after login

Method 4:

In [167]: pat = re.compile(&#39;<[^>]+>&#39;, re.S)
In [168]: pat.sub(&#39;&#39;, test)
Out[168]: &#39;just for testjust for testtest&#39;
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template