Python-Day4 achieves simple shell sed replacement function, python-day4sed

Source: Internet
Author: User

Python-Day4 achieves simple shell sed replacement function, python-day4sed

Code:

1 f = open ('yesterday', 'R', encoding = 'utf-8') 2 f2 = open ('Yesterday. bak ', 'w', encoding = 'utf-8') 3 old_str = input ('Enter the character to modify :') 4 replace_str = input ('Enter the character to replace with: ') 5 for line in f. readlines (): 6 line = line. replace (old_str, replace_str) 7 print (line) 8 f2.write (line) 9 f. close () 10 f2.close ()

File Content: (yesterday)

Somehow, it seems the love I knew was always the most destructive kind Somehow, the love I 've experienced is always The most devastating Yesterday when I was young and frivolous The taste of life was sweet The taste of life is sweet As rain upon my tongue is like a tongue rain on

 

Thoughts:

The idea is very simple: Open the source file, and then loop, replace the content to be replaced by the source file and write it into the new file!

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.