# -*- coding: utf-8 -*-
import os
def alter(file,old_str,new_str,new_file_name):
"""
将替换的字符串写到一个新的文件中
"""
with open(file, "r") as f1,open("%s.bak" % file, "w") as f2:
for line in f1:
if old_str in line:
line = line.replace(old_str, new_str)
f2.write(line)
os.rename("%s.bak" % file, new_file_name)
#将DPS.train文件中的__label__3替换为__label__2,生成新的文件叫DPS_new.train
alter("DPS.train", "__label__3", "__label__2","DPS_new.train")
网友评论