Skip to content Skip to sidebar Skip to footer

Replace Section Of Text With Only Knowing The Beginning And Last Word Using Python

In Python, it possible to cut out a section of text in a document when you only know the beginning and end words? For example, using the bill of rights as the sample document, sear

Solution 1:

You can use Python's re module.

I wrote this example script for removing the sections of code in file:

import re

# Create regular expression pattern
chop = re.compile('#chop-begin.*?#chop-end', re.DOTALL)

# Open file
f = open('data', 'r')
data = f.read()
f.close()

# Chop text between #chop-begin and #chop-end
data_chopped = chop.sub('', data)

# Save result
f = open('data', 'w')
f.write(data_chopped)
f.close()

Solution 2:

With data.txt

do_something_public()

#chop-begin abcd
get_rid_of_me() #chop-end#chop-beginner this should stay!

#chop-begindo_something_private()
#chop-end   The rest of this comment should go too!

but_you_need_me()  #chop-beginlast_to_go()
#chop-end

the following code

import re

classChopper(object):
    def__init__(self, start='\\s*#ch'+'op-begin\\b', end='#ch'+'op-end\\b.*?$'):
        super(Chopper,self).__init__()
        self.re = re.compile('{0}.*?{1}'.format(start,end), flags=re.DOTALL+re.MULTILINE)

    defchop(self, s):
        return self.re.sub('', s)

    defchopFile(self, infname, outfname=None):
        if outfname isNone:
            outfname = infname

        withopen(infname) as inf:
            data = inf.read()

        withopen(outfname, 'w') as outf:
            outf.write(self.chop(data))

ch = Chopper()
ch.chopFile('data.txt')

results in data.txt

do_something_public()

#chop-beginner this should stay!

but_you_need_me()

Solution 3:

Use regular expressions:

import re

string = re.sub('#chop-begin.*?#chop-end', '', string, flags=re.DOTALL)

.*? will match all between.

Post a Comment for "Replace Section Of Text With Only Knowing The Beginning And Last Word Using Python"