code:
f = open('yesterday','r',encoding='utf-8')f2 = open('yesterday.bak','w',encoding='utf-8')old_str = input('請輸入要修改的字符:')replace_str = input('請輸入替換成的字符:')for line in f.readlines(): line = line.replace(old_str,replace_str) print(line) f2.write(line)f.close()f2.close()文件內容:(yesterday)
Somehow, it seems the love I knew was always the most destructive kind不知為何,我經歷的愛情總是最具毀滅性的的那種Yesterday when I was young昨日當我年少輕狂The taste of life was sweet生命的滋味是甜的As rain upon my tongue就如舌尖上的雨露
感想:
思路很簡單就是打開源文件,然后循環,把源文件要替換的內容替換再寫入新文件!
以上這篇Python 實現簡單的shell sed替換功能(實例講解)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持VEVB武林網。
新聞熱點
疑難解答