一个从文本文件里“查找并替换”的功能
生活随笔
收集整理的這篇文章主要介紹了
一个从文本文件里“查找并替换”的功能
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
| 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 |
總結
以上是生活随笔為你收集整理的一个从文本文件里“查找并替换”的功能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 画布 保存_Python
- 下一篇: apache 域名不变_apache 如