脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python 实现简单的shell sed替换功能(实例讲解)

Python 实现简单的shell sed替换功能(实例讲解)

2020-12-10 00:28IT小彭友 Python

下面小编就为大家带来一篇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替换功能(实例讲解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:http://www.cnblogs.com/pjjo/archive/2017/09/28/7608022.html

延伸 · 阅读

精彩推荐