Home  >  Article  >  Backend Development  >  Python implements shell sed replacement simple function

Python implements shell sed replacement simple function

黄舟
黄舟Original
2017-10-02 19:43:581685browse

The following editor will bring you an article on how to implement a simple shell sed replacement function in Python (explanation with examples). The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.

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()

File content: (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
就如舌尖上的雨露

Impressions:

The idea is very simple, just open the source file, then loop, and convert the source file Replace the content to be replaced and write it into a new file!

The above is the detailed content of Python implements shell sed replacement simple function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn