Ai
1 Star 0 Fork 0

Role/Python

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
text_file_replace.py 1.29 KB
一键复制 编辑 原始数据 按行查看 历史
Linjian Li 提交于 2020-10-17 10:02 +08:00 . Wrap it into a function
"""
Author: Linjian Li (github.com/LinjianLi)
Created: 2020-04-09
Last Modified: 2020-10-17
Description: A script that replace specified text string in a text file.
How to use: `-f` specifying the text file,
`-e` specifying the encoding (optional),
`-o` specifying the old text string to be replaced),
`-n` specifying the new text string to replace with.
"""
def text_file_replace(file, encoding, old, new):
lines = []
cnt = 0
with open(file=file, mode='r', encoding=encoding) as fd:
for line in fd:
cnt += line.count(old)
lines.append(line.replace(old, new))
with open(file=file, mode='w', encoding=encoding) as fd:
fd.writelines(lines)
print("{} occurence(s) of \"{}\" have been replaced with \"{}\"".format(cnt, old, new))
return cnt
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-f", "--file", help = "File.")
parser.add_argument("-e", "--encoding", default='utf-8', help = "Encoding.")
parser.add_argument("-o", "--old", help = "Old string.")
parser.add_argument("-n", "--new", help = "New string.")
args = parser.parse_args()
text_file_replace(args.file, args.encoding, args.old, args.new)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/CRole/Python.git
git@gitee.com:CRole/Python.git
CRole
Python
Python
master

搜索帮助