如何使用python在文件的搜索模式的下一行中插入字符串?

时间:2018-07-30 07:22:10

标签: python regex python-2.7

我有一个内容如下的文件:-

He is good at python.
Python is not a language as well as animal.
Look for python near you.
Hello World it's great to be here.

现在,脚本应搜索模式“ python”或“ pyt”或“ pyth”或“ Python”或与“ p / Python”相关的任何正则表达式。搜索特定单词后,应插入新单词,例如“ Lion”。因此输出应如下所示:-

He is good at python.
Lion
Python is not a language as well as animal.
Lion
Look for python near you.
Lion
Hello World it's great to be here.

我该怎么做?

注意:- 直到现在我都写了这样的代码:-

def insertAfterText(args):
    file_name = args.insertAfterText[0]
    pattern = args.insertAfterText[1]
    val = args.insertAfterText[2]
    fh = fileinput.input(file_name,inplace=True)
    for line in fh:
        replacement=val+line
        line=re.sub(pattern,replacement,line)
        sys.stdout.write(line)
    fh.close()

2 个答案:

答案 0 :(得分:2)

与尝试写入现有文件的中间位置相比,最好写一个新文件。

with open是打开文件的最佳方法,因为一旦完成,它就会安全可靠地为您关闭文件。这是使用with open一次打开两个文件的一种很酷的方法:

import re

pattern = re.compile(r'pyt', re.IGNORECASE)

filename = 'textfile.txt'
new_filename = 'new_{}'.format(filename)

with open(filename, 'r') as readfile, open(new_filename, 'w+') as writefile:
    for line in readfile:
        writefile.write(line)
        if pattern.search(line):
            writefile.write('Lion\n')

在这里,我们正在打开现有文件,并打开一个新文件(创建文件)以进行写入。我们遍历输入文件,然后简单地将每一行写到新文件中。如果原始文件中的一行包含与我们的正则表达式模式匹配的内容,则在写入原始行之后,我们还将写入Lion\n(包括换行符)。

答案 1 :(得分:1)

将文件读入变量:

with open("textfile") as ff:
  s=ff.read()

使用正则表达式并将结果写回:

with open("textfile","w") as ff:
  ff.write(re.sub(r"(?mi)(?=.*python)(.*?$)",r"\1\nLion",s))

    (?mi): m: multiline, i.e. '$' will match end of line;
           i: case insensitiv;
    (?=.*python): lookahead, check for "python";
    Lookahead doesn't step forward in the string, only look ahead, so:
    (.*?$) will match the whole line, 
     which we replace with self '\1' and the other.

编辑: 要从命令行插入,请执行以下操作:

import sys
textfile=sys.argv[1]
pattern=sys.argv[2]
newtext=sys.argv[3]

并替换

r"(?mi)(?=.*python)(.*?$)",r"\1\nLion"

使用

fr"(?mi)(?=.*{pattern})(.*?$)",r"\1{newtext}"

并在open()中将“文本文件”更改为文本文件。