1 #编程实现全部替换 2 file_name = input(‘请输入文件名:‘) 3 aim_word = input(‘请输入需要替换的单词或者字符:‘) 4 new_word = input(‘请输入新的单词或者字符:‘) 5 count = 0 6 f = open(file_name) 7 content = [] 8 9 for each_line in f: 10 for each_word in each_line: 11 if each_word == aim_word: 12 count+=1 13 14 each_line=each_line.replace(aim_word,new_word) 15 content.append(each_line) 16 17 f.close() 18 19 print(‘文档共有%d个【%s】‘% (count,aim_word)) 20 print(‘您确定要将【%s】替换为【%s】吗?‘% (aim_word, new_word)) 21 decide = input(‘【Yes/No】:‘) 22 23 if decide == ‘yes‘: 24 g = open(file_name,‘w‘) 25 g.writelines(content) 26 g.close() 27 print(‘恭喜!所有的【%s】已替换完成!‘% aim_word) 28 else: 29 print(‘程序退出!‘)
时间: 2024-12-15 06:10:55