The following small series to bring you a Python implementation of a simple shell sed replacement function (example). Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.
Code
f = open (' Yesterday ', ' R ', encoding= ' utf-8 ') F2 = open (' Yesterday.bak ', ' W ', encoding= ' utf-8 ') old_str = input (' Please enter the character to be modified: ') Replace_str = input (' Please enter a replacement character: ') for line in F.readlines (): line = Line.replace (old_str,replace_str) print ( Line) F2.write (line) F.close () F2.close ()
File contents: (Yesterday)
Somehow, it seems the love I knew is always the most destructive kind I don't know why, I went through the most devastating of the kind yesterday when I am young yesterday when I Young and frivolous The taste's life is sweet as rain upon my tongue like the dew on the tongue
Feelings:
The idea is simple is to open the source file, and then loop, the source file to replace the content to replace and then write new files!