Python-Day4實現簡單的shell sed替換功能
阿新 • • 發佈:2017-09-28
color utf-8 ast style 實現 nes 寫入 readlines lov
code:
1 f = open(‘yesterday‘,‘r‘,encoding=‘utf-8‘) 2 f2 = open(‘yesterday.bak‘,‘w‘,encoding=‘utf-8‘) 3 old_str = input(‘請輸入要修改的字符:‘) 4 replace_str = input(‘請輸入替換成的字符:‘) 5 for line in f.readlines(): 6 line = line.replace(old_str,replace_str) 7 print(line) 8 f2.write(line) 9 f.close()10 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-Day4實現簡單的shell sed替換功能