2

I would like to replace text in a file using a regexp and Python. Using sed I can can do something like this on the command line

sed -r 's/([0-9]{1,3}\.)([0-9]{1,3}\.)([0-9]{1,3}\.)([0-9]{1,3})/\1\2xx.xx/' ./input/my_file > ./output/my_file_new

Which basically takes looks for a string of ip=[4 octets] and replaces the last two with xx.

The input file would look like

name=rockband&ip=176.4.23.71&releasedate=none
name=rockband2&ip=121.1.44.52&releasedate=none

The desired output file looks like

name=rockband&ip=176.4.xx.xx&releasedate=none
name=rockband2&ip=121.1.xx.xx&releasedate=none

I need to put this into a Python script I am using

import re
regexp = re.compile(r's/([0-9]{1,3}\.)([0-9]{1,3}\.)([0-9]{1,3}\.)([0-9]{1,3})/\1\2xx.xx/')

def replace(source_file_path):
fh, target_file_path = mkstemp()

with codecs.open(target_file_path, 'w', 'utf-8') as target_file:
    with codecs.open(source_file_path, 'r', 'utf-8') as source_file:
        for line in source_file:
            print(line)
            target_file.write( !! How to use sub in here )
remove(source_file_path)
move(target_file_path, source_file_path)

How can I use the sub() method to achieve what I want to do? I need to pass 3 arguments to this method and can only think of how to pass 2, I don't know what that third argument should be

target_file.write(re.sub(regexp, line))
3
  • Uhm, Sample I/O? Commented Jan 29, 2019 at 12:37
  • So replacing every digit after third . into a x before the first &? Commented Jan 29, 2019 at 12:49
  • Unlike in case of sed syntax, your sub expect what to match and what to replace it as two arguments. Commented Jan 29, 2019 at 12:49

1 Answer 1

2

Minimal required change to you code would be:

import re
regexp = re.compile(r'([0-9]{1,3}\.)([0-9]{1,3}\.)([0-9]{1,3}\.)([0-9]{1,3})')

def replace(source_file_path):
    fh, target_file_path = mkstemp()

    with codecs.open(target_file_path, 'w', 'utf-8') as target_file:
        with codecs.open(source_file_path, 'r', 'utf-8') as source_file:
            for line in source_file:
                print(line)
                target_file.write(regexp.sub(r'\1\2xx.xx', line))
    remove(source_file_path)
    move(target_file_path, source_file_path)

regexp only defines what to match. sub() has an argument of what to substitute with.

You can either call re.sub() which takes three required arguments: what to match, what to replace it with, which string to work on. Or as in the example above when you already have a precompiled regex, you can use its sub() method in which case need to say what to replace with and what string to work on.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.