我有一个文本文件,我正在读取它:
with open(file,'r+') as f:
file_data = f.read()
file_data
是一个长字符串,包含以下文本:
'''This file starts here dig_1 = hellon doge ras = friendn sox = pien'''
我想搜索dig_1
,然后获得'='
之后直到换行符n
的所有文本,并将其替换为不同的文本,使其成为dig_1 = hellon
现在是dig_1 = unknown
,并对其他文本(ras = friendn
到ras = unknown
和sox = pien
到sox = unknown
(执行相同操作。使用regex有没有一种简单的方法可以做到这一点?
您可以使用python的re
模块的sub
函数
要替换的模式看起来像一个后面跟着等号和空格的单词,并且前面还有一个换行符
# import re module
import re
# original text
txt = '''This file starts here dig_1 = hellon doge ras = friendn sox = pien'''
# pattern to look for
pattern = '= (.*?n)'
# string to replace with
repl = 'unknown'
# replace 'pattern' with the string inside 'repl' in the string 'txt'
re.sub(pattern, repl, txt)
'This file starts here dig_1 unknown doge ras unknown sox unknown'
您可以在此处使用re.sub
:
inp = "This file starts here dig_1 = hellon doge ras = friendn sox = pien"
output = re.sub(r'b(S+)s*=s*.*(?=n|$)', r'1 = unknown', inp)
print(output)
此打印:
This file starts here dig_1 = unknown
doge ras = unknown
sox = unknown