简体   繁体   中英

How to search and replace a string in place in a file

I am trying to find and replace in place a particular string from my input file.

sample.txt

// This is header                                                                           
// *  Copyright 2017-2018, Company name  *
// * abc      : TBD                      *

Code:

import re
import fileinput

with fileinput.FileInput("sample.txt", inplace=True, backup='.bak') as file:
    for line in file:
        m = re.search('.*Copyright\s+(.+?),\s+Company', line, re.MULTILINE|re.DOTALL)
        if m:
            found = m.group(1)
            print(line.replace(found,'2021', line.rstrip()))

Error:

Traceback (most recent call last):
  File "tmp.py", line 23, in <module>
    print(line.replace(found,'2021', line.rstrip()))
TypeError: 'str' object cannot be interpreted as an integer

Expected output sample.txt:

// This is header                                                                           
// *  Copyright 2021, Company name  *
// * abc      : TBD                 *

Not sure exactly what is wrong in this code. Can someone help me fixing this? Thanks in advance!

The str.replace() method takes in 3 parameters:

  • the substring to be removed and replaced with another

  • the substring to replace the removed substring

  • the maximum amount of substrings to replace

As you can see, the third parameter can only be an integer, yet in print(line.replace(found,'2021', line.rstrip())) , line.rstrip() is a string.

It should be:

print(line.rstrip().replace(found, '2021'))

The third argument to replace() is the maximum number of replacements to make, which must be an integer. The string that you're operating on is not an argument, it's specified before the method name.

Also, you need to print the line without replacing anything if it doesn't match the regexp, so that the unmatched lines are written back to the file.

with fileinput.FileInput("sample.txt", inplace=True, backup='.bak') as file:
    for line in file:
        m = re.search('.*Copyright\s+(.+?),\s+Company', line)
        if m:
            found = m.group(1)
            print(line.rstrip().replace(found, '2021'))
        else:
            print(line.rstrip())

And there's no need for re.MULTILINE and re.DOTALL flags, since you're only processing one line at a time.

Try this:

import re

with open("sample.txt", "r") as file:
    for line in con.split("\n"):
        m = re.sub('.*Copyright\s+(.+?),\s+Company', "2021", line)
        if m:
            print(m)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM