简体   繁体   English

打印从字符串1到字符串2的后x行

[英]Print next x lines from string1 until string2

I'm trying to write a function that reads through a text file until it finds a word (say "hello"), then print the next x lines of string starting with string 1 (say "start_description") until string 2 (say "end_description"). 我正在尝试编写一个读取文本文件的函数,直到找到一个单词(例如“ hello”),然后打印从字符串1(例如“ start_description”)开始的字符串的后x行,直到字符串2(例如“ end_description”)。

hello

start_description 123456 end_description

The function should look like description("hello") and the following output should look like 该函数应类似于description(“ hello”),并且以下输出应类似于

123456

It's a bit hard to explain. 有点难以解释。 I know how to find the certain word in the text file but I don't know how to print, as said, the next few lines between the two strings (start_description and end_description). 我知道如何在文本文件中找到某个单词,但是我不知道如何打印两个字符串之间的下几行(start_description和end_description)。

EDIT1: I found some code which allows to print the next 8, 9, ... lines. EDIT1:我发现一些代码可以打印接下来的8、9,...行。 But because the text in between the two strings is of variable length, that does not work... 但是因为两个字符串之间的文本长度可变,所以不起作用...

EDIT2: Basically it's the same question as in this post: Python: Print next x lines from text file when hitting string , but the range(8) does not work for me (see EDIT1). EDIT2:基本上是与本文相同的问题: Python:击中string时从文本文件中打印下x行 ,但range(8)对我不起作用(请参阅EDIT1)。

The input file could look like: 输入文件如下所示:

HELLO
salut
A: 123456.

BYE
au revoir
A: 789123.

The code should then look like: 该代码应如下所示:

import re
def description(word):
    doc = open("filename.txt",'r')
    word = word.upper()

    for line in doc:
        if re.match(word,line):
            #here it should start printing all the text between start_description and end_description, for example 123456

    return output

print description("hello")
123456
print description("bye")
789123

Here's a way using split: 这是使用split的一种方法:

start_desc = 'hello'
end_desc = 'bye'
str = 'hello 12345\nabcd asdf\nqwer qwer  erty\n bye'

print str.split('hello')[1].split('bye')[0]

The first split will result in: 第一次拆分将导致:

('', ' 12345\nabcd asdf\nqwer qwer  erty\n bye')

So feed the second element to the second split and it will result in: 因此,将第二个元素提供给第二个拆分,它将导致:

('12345\nabcd asdf\nqwer qwer  erty\n ', '')

Use the first element. 使用第一个元素。

You can then use strip() to remove the surrounding spaces if you wish. 然后,您可以根据需要使用strip()删除周围的空间。

def description(infilepath, startblock, endblock, word, startdesc, enddesc):
    with open(infilepath) as infile:
        inblock = False
        name = None
        found = False
        answer = []
        for line in infile:
            if found and not inblock: return answer
            if line.strip() != startblock and not inblock: continue
            if line.strip() == startblock: inblock = True
            elif line.strip() == endblock: inblock = False
            if not line.startswith(startdesc):
                name = line.strip()
                continue
            if name is not None and name != word: continue
            if not line.startswith(startdesc): continue
            answer.append(line.strip().lstrip(startdesc).rstrip(enddesc))

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

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