Python implements shell sed replacement simple function

黄舟
Release: 2017-10-02 19:43:58
Original
1619 people have browsed it

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()
Copy after login

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
就如舌尖上的雨露
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!