自定义replace函数,模仿shell的sed替换
Posted 仙寓游子
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自定义replace函数,模仿shell的sed替换相关的知识,希望对你有一定的参考价值。
import sys
"""
Usages:
1. command line: python replacestr.py para1 para2 para3;
2. para1 : file_name
para2 : original string
para3 : new string
"""
"""
Usages:
1. command line: python replacestr.py para1 para2 para3;
2. para1 : file_name
para2 : original string
para3 : new string
"""
file_name = sys.argv[1]
find_str = sys.argv[2]
replace_str = sys.argv[3]
find_str = sys.argv[2]
replace_str = sys.argv[3]
f=""
with open(file_name, "r") as fs1:
for n in fs1:
f += n.replace(find_str, replace_str)
with open(file_name, "w") as fs2:
fs2.write(f)
fs2.flush()
with open(file_name, "r") as fs1:
for n in fs1:
f += n.replace(find_str, replace_str)
with open(file_name, "w") as fs2:
fs2.write(f)
fs2.flush()
以上是关于自定义replace函数,模仿shell的sed替换的主要内容,如果未能解决你的问题,请参考以下文章