我想使用正则表达式和 Python 替换文件中的文本。使用 sed 我可以在命令行上执行类似的操作
sed -r 's/([0-9]{1,3}\.)([0-9]{1,3}\.)([0-9]{1,3}\.)([0-9]{1,3})/\1\2xx.xx/' ./input/my_file > ./output/my_file_new
这基本上需要查找 ip=[4 个八位字节] 的字符串并用 xx 替换最后两个。
输入文件看起来像
name=rockband&ip=176.4.23.71&releasedate=none
name=rockband2&ip=121.1.44.52&releasedate=none
所需的输出文件如下所示
name=rockband&ip=176.4.xx.xx&releasedate=none
name=rockband2&ip=121.1.xx.xx&releasedate=none
我需要将其放入我正在使用的 Python 脚本中
import re
regexp = re.compile(r's/([0-9]{1,3}\.)([0-9]{1,3}\.)([0-9]{1,3}\.)([0-9]{1,3})/\1\2xx.xx/')
def replace(source_file_path):
fh, target_file_path = mkstemp()
with codecs.open(target_file_path, 'w', 'utf-8') as target_file:
with codecs.open(source_file_path, 'r', 'utf-8') as source_file:
for line in source_file:
print(line)
target_file.write( !! How to use sub in here )
remove(source_file_path)
move(target_file_path, source_file_path)
如何使用sub()
方法来实现我想要做的事情?我需要向这个方法传递3个参数,只能想到如何传递2个,我不知道第三个参数应该是什么
target_file.write(re.sub(regexp, line))
最佳答案
对代码所需的最小更改是:
import re
regexp = re.compile(r'([0-9]{1,3}\.)([0-9]{1,3}\.)([0-9]{1,3}\.)([0-9]{1,3})')
def replace(source_file_path):
fh, target_file_path = mkstemp()
with codecs.open(target_file_path, 'w', 'utf-8') as target_file:
with codecs.open(source_file_path, 'r', 'utf-8') as source_file:
for line in source_file:
print(line)
target_file.write(regexp.sub(r'\1\2xx.xx', line))
remove(source_file_path)
move(target_file_path, source_file_path)
regexp
仅定义要匹配的内容。 sub()
有一个关于替换内容的参数。
您可以调用 re.sub()
它需要三个必需的参数:匹配什么、替换什么、处理哪个字符串。或者如上面的示例所示,当您已经有预编译的正则表达式时,您可以使用它的 sub()
方法在这种情况下需要说明要替换什么以及要处理什么字符串。
关于python - 在 python 中使用 sub() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54421220/