Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
345 views
in Technique[技术] by (71.8m points)

python - 根据模式在文件中查找字符串,然后用其他字符串替换(Find a string in a file based on a pattern and replace it with something else)

In a file, I want to find a specific content(lines or lines) based on a string pattern, make changes to it, and replace it.

(在文件中,我想根据字符串模式查找特定的内容(行或行),对其进行更改并替换。)

The pattern can appear multiple times.

(该图案可以出现多次。)

The file can contain code, (python or c).

(该文件可以包含代码(python或c)。)

The string pattern can have multiple forms so I thought using regex, like:

(字符串模式可以有多种形式,所以我认为使用正则表达式,例如:)

custom_log("lorem ipsum can be anything ....")

or

(要么)

custom_log("lorem ipsum"
           "can be anything") 

or

(要么)

custom_log("""lorem ipsum
           can be anything""") 

The quotes can be simple or double.

(引号可以是简单的也可以是双引号。)

I start by going line by line, and search for a pattern

(我从逐行开始,然后搜索模式)

with open(filepath, mode="r") as f:
  for line in f.readlines():
       if "pattern" in line

but the issue, is that the content that I search it can be one one line or multiple lines, and between quotes can be anything as a string.

(但是问题是,我搜索的内容可以是一行或多行,而引号之间可以是字符串。)

I can't use a simple replace, because I need to get the content, passed to a function, change/adapt the content and pass it back to be written to file.

(我不能使用简单的替换,因为我需要获取内容,将其传递给函数,更改/调整内容,然后将其传递回回写入文件。)

I want to keep the initial formatting of the file.

(我想保留文件的初始格式。)

  ask by user3541631 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This question needs more concrete examples of what is desired, so I'll give an example where a match and replacement could span multiple lines.

(这个问题需要更具体的示例,因此我将给出一个示例,其中匹配和替换可能跨越多行。)

Given the following sample input, sample.txt :

(给定以下样本输入sample.txt :)

the quick brown fox jumped over the lazy dog
the quick
brown fox
jumped over
the lazy dog

The following code will replace certain pairs of words even if broken across lines using re.sub and a lambda function for processing the replacement:

(以下代码将替换某些单词对,即使使用re.sub和用于处理替换项的lambda函数re.sub之间打断也是如此:)

import re

with open('sample.txt') as f:
    data = f.read()

def replace(m):
    return ''.join([c if c.isspace() else '*'
                    for c in m.group(0)])

data = re.sub(r'quicks+brown|overs+the',replace,data)
print(data)

Output:

(输出:)

the ***** ***** fox jumped **** *** lazy dog
the *****
***** fox
jumped ****
*** lazy dog

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...