如何复制 python 脚本中标记后面的行?

发布于 2024-11-30 05:17:25 字数 522 浏览 1 评论 0原文

我试图在某个标记之后复制行一定次数。

如果我有一个如下所示的文本文件:

##TextLines##
Hi
Hello
##TextLines##

如何删除标签并复制该行一次?

最终文本文件如下所示:

Hi
Hello
Hi
Hello

现在,我有一个正则表达式来查找标签并将其替换为空白换行符。我知道大多数逐行读取是在打开文件后使用 for 循环完成的。但是,我不想处理读取的特定行,而是处理后面的行。有什么想法吗?

编辑:可能有多个标签和未标记的文本。例如:

Hi
##CopyLine1##
Hello
##CopyLine1##
Greetings
##CopyLine2##
Howdy
##CopyLine2##
Hola

将变为:

Hi
Hello
Hello
Greetings
Howdy
Howdy
Hola

I am trying to copy lines a certain number of times following a certain tag.

If I have a text file like the following:

##TextLines##
Hi
Hello
##TextLines##

How do I erase the tags and copy the line once?

The end text file would look like:

Hi
Hello
Hi
Hello

Right now, I have a regular expression to find the tags and replace them with a blank newline character. I know most line-by-line reading is done using a for loop after opening a file. However, I don't want to process the specific line read, but the lines after. Any ideas?

Edit: There could be multiple tags and untagged text. For instance:

Hi
##CopyLine1##
Hello
##CopyLine1##
Greetings
##CopyLine2##
Howdy
##CopyLine2##
Hola

would become:

Hi
Hello
Hello
Greetings
Howdy
Howdy
Hola

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荒芜了季节 2024-12-07 05:17:25

这应该可以完成工作

import re 
regex = re.compile("^##.*##\n$")
out = open("result.txt","w")
matchfound = True
inmatch =False
for line in open('myfile.txt'):
   if regex.match(line):
       matchfound = True
   else:
        matchfound = False
   if matchfound and not inmatch:
       inmatch = True
       content = []
   elif matchfound and inmatch:
       inmatch = False
       out.write ("".join(content))
   elif inmatch:
       content.append(line)
       out.write(line)
   else:
       out.write(line)
out.close()

This should do the job

import re 
regex = re.compile("^##.*##\n$")
out = open("result.txt","w")
matchfound = True
inmatch =False
for line in open('myfile.txt'):
   if regex.match(line):
       matchfound = True
   else:
        matchfound = False
   if matchfound and not inmatch:
       inmatch = True
       content = []
   elif matchfound and inmatch:
       inmatch = False
       out.write ("".join(content))
   elif inmatch:
       content.append(line)
       out.write(line)
   else:
       out.write(line)
out.close()
我喜欢麦丽素 2024-12-07 05:17:25
def isTag(line):
    return line.startswith('##')

class LineHandler(object):
    def normalLine(self,line):
        if isTag(line):
            self.lineHandler = self.insideTag
        else:
            print line

    def insideTag(self,line):
        if isTag(line):
            self.lineHandler = self.normalLine
        else:
            print line
            print line

    def __init__(self,path):
        self.lineHandler = self.normalLine
        for line in file(path):
            self.lineHandler(line.strip())

LineHandler('lines.txt')
def isTag(line):
    return line.startswith('##')

class LineHandler(object):
    def normalLine(self,line):
        if isTag(line):
            self.lineHandler = self.insideTag
        else:
            print line

    def insideTag(self,line):
        if isTag(line):
            self.lineHandler = self.normalLine
        else:
            print line
            print line

    def __init__(self,path):
        self.lineHandler = self.normalLine
        for line in file(path):
            self.lineHandler(line.strip())

LineHandler('lines.txt')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文