Python enables shell sed to replace simple functions

Source: Internet
Author: User
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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.