Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 复制两个非´;t位于行的开头_Python_Python 3.x_String - Fatal编程技术网

Python 复制两个非´;t位于行的开头

Python 复制两个非´;t位于行的开头,python,python-3.x,string,Python,Python 3.x,String,我是这个世界的新手,我需要用这种文件: ……| 4751 | . . . . . . 99999| 我要搜索此字符串“| 4751 |”,并复制所有行以及带有字符串“| 4751 |”的行,直到包含99999的行| 文本有多个“| 4751 |”和99999 |,我想复制它们之间的所有行 这在python中是可能的吗?我在这里找到了一些帖子,但是问题是我不能适应这个脚本,因为我的字符串4751是在行的中间,而不是在它的开头。 你可以使用 R.FundU/使用 R.MultLay.Re

我是这个世界的新手,我需要用这种文件:


……| 4751 |

.
.
.
. . . 99999|

我要搜索此字符串“| 4751 |”,并复制所有行以及带有字符串“| 4751 |”的行,直到包含99999的行|

文本有多个“| 4751 |”和99999 |,我想复制它们之间的所有行


这在python中是可能的吗?我在这里找到了一些帖子,但是问题是我不能适应这个脚本,因为我的字符串4751是在行的中间,而不是在它的开头。

你可以使用<代码> R.FundU/<代码>使用<代码> R.MultLay.Re.doasy< /Calp>,和一个前瞻。确保转义
|
字符,这些字符在正则表达式中具有特殊含义:

import re

haystack = """foobar0
foo|4751|bar1
foobar2
foo99999|bar3
foobar4
foobar5
foo|4751|bar6
foo99999|bar7
foobar8"""

needle = "^[^\n]*\|4751\|.*?(?=99999\|)"
result = re.findall(needle, haystack, re.MULTILINE|re.DOTALL)

print (result)
其结果是:


我们可以构建一个自定义迭代器(和上下文管理器),从输入文件中检索所需的行

class Collector:
    def __init__(self, filename, start_marker, stop_marker):
        self.toggle_collect = False
        self.f = open(filename)
        self.m1 = start_marker
        self.m2 = stop_marker

    def __enter__ (self):
        return self

    def __exit__ (self, exc_type, exc_value, traceback):
        self.f.close()

    def __iter__(self):
        return self

    def __next__(self):
        while True:
            r = next(self.f)
            if self.m1 in r:  # found the start-collecting marker
                self.toggle_collect = True
            elif self.m2 in r:  # found the stop-collecting marker
                self.toggle_collect = False
                continue
            if self.toggle_collect:  # we are collecting
                return r.rstrip()  # collect row


with Collector('file_rows.txt', '4751', '99999') as c:
    for r in c:
        print(r)
其中,输入文件为

foobar0
foo|4751|bar1
foobar2
foo99999|bar3
foobar4
foobar5
foo|4751|bar6
foo99999|bar7
foobar8
产生

foo|4751|bar1

foobar2

foo|4751|bar6
注意:正如您在输出中所看到的,行之间用空行隔开。这是因为它们包括了回车

如果不需要,我们可以把它脱掉

            if self.toggle_collect:  # we are collecting
                return r.rstrip()  # provide row
如果我们想要列表中的行,只需从迭代器创建一个即可

with Collector('file_rows.txt', '4751', '99999') as c:
    results = list(c)

文本在文件中吗?既然你说你是新来的,如果文本在一个文件中,那么这种特定的工作最好使用
perl
而不是
python
来完成。我并不是说python不能做到这一点,而是说这在perl中是一个快速而简单的文件。它是一个.txt文件。在这种情况下,如果使用
perl-ne“print if/\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\/../.*9999/”dat.txt>dat1.txt
将解决您的问题。当然,您可以在命令行中执行此操作,其中
dat.txt
existhanks,稍后我将尝试此操作。比方说,我不希望9999出现在我的最后一个txt中,只是搜索在那里停止,然后转到| 4751 |,然后复制到下一个9999 |,但不将9999本身复制到最终文件中。我应该怎么做在您的代码?谢谢你的帮助help@eduardomb我编辑了答案来解释这一点。如果对你有帮助,别忘了。我对电脑很陌生。由于print命令,我可以在屏幕上看到结果,但是如何将我想要的行保存在一个.txt文件中呢?感谢您的回答Edit2:我成功地将带有“write”(%s\n“%r”)的保存为列表。非常感谢你的帮助(最后一个问题。这个脚本可以同时使用多个txt吗?当然可以,但问题的主题正在增加,并成为一个咨询。我的费率是……:)私下写信给我,描述你想要的输入/输出,这样我可以为你指出正确的解决方案
with Collector('file_rows.txt', '4751', '99999') as c:
    results = list(c)