一个从文本文件里“查找并替换”的功能
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
# -*- coding: UTF-8 -*- file = input ( "请输入文件路径:" )
word1 = input ( "请输入要替换的词:" )
word2 = input ( "请输入新的词:" )
f = open ( file , "r+" )
AAA = f.read()
count = 0
def BBB():
global count
for each in AAA:
if each = = word1:
count = count + 1
print ( "一共有【" + str (count) + "】处要找的【" + str (word1) + "】" )
BBB() #红色这一段其实是最最容易蒙圈的地方# print ( "您确定要把所有的【" + str (word1) + "】替换为【" + str (word2) + "】吗?" )
K = input ( "【YES/NO】:" )
AAA = AAA.replace(word1,word2)
while True :
if K = = "YES" or K = = "yes" :
print (AAA)
break
else :
print ( "操作取消!" )
break
|
本文转自 苏幕遮618 51CTO博客,原文链接:http://blog.51cto.com/chenx1242/1728957