简体   繁体   English

如何在python中打印下一行

[英]How to print next line in python

I am trying to print next 3 lines after a match 我正在尝试在比赛后打印接下来的3行

for example input is : 例如输入是:

Testing
Result
test1 : 12345
test2 : 23453
test3 : 2345454

so i am trying to search "Result" string in file and print next 3 lines from it: 所以我试图在文件中搜索“结果”字符串并从中打印下3行:

Output will be :- 输出将是:

test1 : 12345
test2 : 23453
test3 : 2345454

my code is : 我的代码是:

with open(filename, 'r+') as f:
    for line in f:
        print line
        if "Benchmark Results" in f:
            print f
            print next(f)

its only giving me the output : 它只给我输出:

testing

how do i get my desired output, help please 我如何获得所需的输出,请帮助

First you need to check that the text is in the line (not in the fileobj f ), and you can utilise islice to take the next 3 lines from f and print them, eg: 首先,您需要检查文本是否在该line (而不是在fileobj f ),然后可以使用islicef islice下3行并进行打印,例如:

from itertools import islice

with open(filename) as f:
    for line in f:
        if 'Result' in line:
            print(''.join(islice(f, 3)))

The loop will continue from the line after the three printed. 循环将在打印三张记录后从该行继续。 If you don't want that - put a break inside the if . 如果你不想这样-在if里面放一点break

You are testing (and printing) "f" instead of "line". 您正在测试(并打印)“ f”而不是“ line”。 Be careful about that. 请注意这一点。 'f' is the file pointer, line has your data. “ f”是文件指针,行包含您的数据。

  with open(filename, 'r+') as f:
      line = f.readline()
      while(line):
          if "Benchmark Results" in line:
               # Current line matches, print next 3 lines
               print(f.readline(),end="")
               print(f.readline(),end="")
               print(f.readline(),end="")
          line = f.readline()

I would suggest opening the file and spliting its content in lines, assigning the outcome to a variable so you can manipulate the data more comfortably: 我建议打开文件并将其内容分成几行,将结果分配给变量,以便您可以更舒适地操作数据:

file = open("test.txt").read().splitlines()

Then you can just check which line contains the string "Result", and print the three following lines: 然后,您可以检查哪一行包含字符串“ Result”,并打印以下三行:

for index, line in enumerate(file):
    if "Result" in line:
        print(file[index+1:index+4])

It is waiting for the first "Result" in the file and then prints the rest of the input: 它正在等待文件中的第一个“结果”,然后打印其余输入:

import re, sys

bool = False
with open("input.txt", 'r+') as f:
    for line in f:
        if bool == True:
            sys.stdout.write(line)
        if re.search("Result",line):    #if it should match whole line, than it is also possible if "Result\n" == line: 
            bool = True

If you want end after first 3 prints, you may add variable cnt = 0 and change this part of code (for example this way): 如果要在前3次打印后结束,可以添加变量cnt = 0并更改这部分代码(例如,以这种方式):

        if bool == True:
            sys.stdout.write(line)
            cnt = cnt+1
            if cnt == 3:
                break
with open('data', 'r') as f:
    lines = [ line.strip() for line in f]
    # get "Result" index
    ind = lines.index("Result")
    # get slice, add 4 since upper bound is non inclusive
    li = lines[ind:ind+4]
    print(li)
    ['Result', 'test1 : 12345', 'test2 : 23453', 'test3 : 2345454']

or as exercise with regex: 

import re  
with open('data', 'r') as f:

    text = f.read()
    # regex assumes that data as shown, ie, no blank lines between 'Result'
    # and the last needed line.
    mo =  re.search(r'Result(.*?\n){4}', text, re.MULTILINE|re.DOTALL)

    print(mo.group(0))

Result

test1 : 12345

test2 : 23453

test3 : 2345454

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

相关问题 Python中a.txt文件如何打印到下一行 - How to print to the next line in a .txt file in Python 如何在python3中打印匹配字符串的下一行(如果我给Tea,它也应该打印下一行) - how to print next line of the match string in python3 (If i give Tea , it should print next line too) 如何使用Python中.format()删除空格和打印线到下一列? - How to remove spaces and print line to the next column using .format() in python? 如何在文本文件中查找一个单词并打印 Python 下一行中的另一个单词 - How to find a word in a text file and print another that is in the next line in Python 如何使用python在正则表达式搜索后打印下一行 - How to print next line after regex search using python 如何使用pdfplumber提取的文本在Python中打印下一行 - How to print the next line in Python with text extracted using pdfplumber 如何从文本文件python中打印下一行 - How to print the next line from a text file python 如何在文件的每一行中搜索单词并在 python 中打印它的下一个值 - How to search for the word in each line of a file and print the next value of it in python Python 打印格式进行到下一行 - Python print formatting carries to next line 如何在Django的下一行打印列表数据 - How to print list data in next line in Django
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM