Python 实现简单的shell sed替换功能(实例讲解)
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 就如舌尖上的雨露
感想:
思路很简单就是打开源文件,然后循环,把源文件要替换的内容替换再写入新文件!
相关推荐
lianshaohua 2020-10-20
thickbookszone 2020-10-16
rechanel 2020-11-16
以梦为马不负韶华 2020-10-20
tianhuak 2020-11-24
huha 2020-10-16
lianshaohua 2020-09-23
laisean 2020-11-11
zhangjie 2020-11-11
大牛牛 2020-10-30
firefaith 2020-10-30
liguojia 2020-10-20
wangzhaotongalex 2020-10-20
CARBON 2020-10-20
彼岸随笔 2020-10-20
yutou0 2020-10-17
JohnYork 2020-10-16
xiaonamylove 2020-10-16
Julyth 2020-10-16