繁体   English   中英

使用Python仅知道开头和结尾的单词来替换文本部分

[英]Replace section of text with only knowing the beginning and last word using Python

在Python中,当您只知道开头和结尾的单词时,可以在文档中切出一段文本吗?

例如,使用权利清单作为样本文档,搜索“修订3”并删除所有文本,直到您单击“修订4”,而实际上并不知道或不关心两个端点之间存在什么文本。

我问的原因是,当我将其他Python程序上传到客户端计算机时,我想使用该Python脚本来修改我的其他Python程序-删除“#chop-begin”和“ #斩结束”。 我不希望客户在不支付更好版本的代码的情况下访问所有功能。

您可以使用Python的re模块。

我写了这个示例脚本来删除文件中的代码部分:

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()

使用正则表达式

import re

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

.*? 将所有之间都匹配。

与data.txt

do_something_public()

#chop-begin abcd
get_rid_of_me() #chop-end

#chop-beginner this should stay!

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

but_you_need_me()  #chop-begin  
last_to_go()
#chop-end

以下代码

import re

class Chopper(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)

    def chop(self, s):
        return self.re.sub('', s)

    def chopFile(self, infname, outfname=None):
        if outfname is None:
            outfname = infname

        with open(infname) as inf:
            data = inf.read()

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

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

结果为data.txt

do_something_public()

#chop-beginner this should stay!

but_you_need_me()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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