本文摘自php中文网,作者黄舟,侵删。
下面小编就为大家带来一篇Python 实现简单的shell sed替换功能(实例讲解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧code:
1 2 3 4 5 6 7 8 9 10 | 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)
1 2 3 4 5 6 7 8 | 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替换简单的功能的详细内容,更多文章请关注木庄网络博客!!
相关阅读 >>
嵌入式与Python选哪个
女生学Python难吗
Python怎么发音
Python什么是递归?两种优先搜索算法的实现 (代码示例)
Python类能调用实例方法吗
Python //什么意思
Python怎么判断数据类型
Python input()函数怎么用
Python怎么计算加减乘除
Python可以开发什么
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » Python实现shell sed替换简单的功能