繁体   English   中英

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

[英]How insert a string in next line for searched pattern in a file using python?

我有一个包含目录的文件,如下所示:-

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

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

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 (包括换行符)。

将文件读入变量:

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()中将“文本文件”更改为文本文件。

暂无
暂无

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

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