Home > other >  Turn to the great god! I want to change the text of a letter to replace all the letters between a an
Turn to the great god! I want to change the text of a letter to replace all the letters between a an

Time:02-21

The code is as follows:
The import re
The import string
The import OS

The file=open (r 'C: \ Users \ Administrator \ Desktop \ \ new folder aa TXT', 'r')

# # lines=file. Readlines () according to the line read

S=file. The read ()

String="'. Join (s)

Result=re. The.findall (" aaa "+" (. *?) "+" CCC ", s)

# print (result)

While len (result) & gt; 0:

A=result. Pop (0)

DD=(s.r eplace (A, ' ') # to replace the original text is empty

# print (result)

# print (DD)

The save=open (r 'C: \ Users \ Administrator \ Desktop \ \ new folder bb TXT', 'w')

Save. Write (DD)

Save the close ()

===================================================================================
The contents of a text and the resulting text b as shown in figure:

Failed to achieve full replacement, really can't find the reason,,

===================================================================================
Small white at first contact, ask the great spirit guide

CodePudding user response:

The greed of the re in matching greed matching try instead

CodePudding user response:

Even at the same time set up re matching labels: re S

CodePudding user response:

 
The import re


The file=open (r 'C: \ Users \ admin \ Desktop \ aa TXT', 'r')

Lines=file. Readlines () line # according to read
Result=""
For the line in lines:
Result +=re. Sub (r '(aaa) (CCC)' (. *), r ', 1, 3, line, flags.=re S)

The save=open (r 'C: \ Users \ admin \ Desktop \ bb TXT', 'w')

Save. Write (result)

Save the close ()
  • Related