Python Basic _ full-text or single-line replacement of file operations,

Source: Internet
Author: User

Python Basic _ full-text or single-line replacement of file operations,

When python modifies a file, the original file is cleared/overwritten in w mode. You can use the read (r) method to open it, write it to the memory, and then use the write (w) method to open it.

1. Replace taste in the text with tasting

Yesterday when I was young and frivolous The taste of life was sweet life taste is sweet As rain upon my tongue
# Read the file to the memory with open (". /fileread.txt "," r ", encoding =" UTF-8 ") as f: lines = f. readlines () # open the file with open (". /fileread.txt "," w ", encoding =" UTF-8 ") as f_w: for line in lines: if" taste "in line: # Replace line = line. replace ("taste", "tasting") f_0000write (line)

2. Search replacement or single line replacement in full text

# Text content Yesterday when I was young and frivolous The taste of life was sweet taste of life is sweet As rain upon my tonguetastetastetastetaste
# Define a function, with four parameters # x indicates the name of the file to be updated # y indicates the content to be replaced # z indicates the content to be replaced # s default parameter 1 indicates that only the first matching is replaced string # If the parameter is s = 'G', it indicates that def string_switch (x, y, z, s = 1): with open (x, "r", encoding = "UTF-8") as f: # readlines read the file lines = f in the form of a list. readlines () with open (x, "w", encoding = "UTF-8") as f_w: # define a number, it is used to record the position n = 0 # default option in the list when reading the file. It only replaces the string if s = 1: for line in lines: if y in line: line = line. replace (y, z) f_0000write (line) n + = 1 break f_0000write (line) n + = 1 # output the remaining text content for I in range (n, len (lines): f_0000write (lines [I]) # Replace elif s = 'G': for line in lines: if y in line: line = line. replace (y, z) f_0000write (line)

Test

1) The default parameter 1 is used to replace the matched first line.

String_switch ("fileread.txt", "taste", "tasting ") # result Yesterday when I was young and frivolous, The taste of The tasting of life was sweet As rain upon my tonguetastetastetastetaste

2) Global replacement

String_switch ("fileread.txt", "taste", "tasting", "g ") # result Yesterday when I was young and frivolous, The taste of The tasting of life was sweet As rain upon my tonguetastingtastingtastingtasting

The above python Basic _ file operation implementation full text or single line replacement method is all the content shared to you by xiaobian, hope to give you a reference, but also hope you can support a lot of help homes.

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.