python中创建列表怎么打下一行,如何在python中打印下一行 (How to print next line in python)...

最佳答案

英文原文

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:

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.

中文翻译

首先,您需要检查文本是否在行中(而不是在fileobj f中),并且您可以使用islice来从f中取出接下来的3行并打印出来,例如:

来自itertools import islice的使用open(filename)作为f:

对于f中的行:

如果'结果'在行:

print(''。join(islice(f,3)))

循环将从三行打印后继续。如果您不想这样 - 在中放入break,如果。

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:

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.

首先,您需要检查文本是否在行中(而不是在fileobj f中),并且您可以使用islice来从f中取出接下来的3行并打印出来,例如:

来自itertools import islice的使用open(filename)作为f:

对于f中的行:

如果'结果'在行:

print(''。join(islice(f,3)))

循环将从三行打印后继续。如果您不想这样 - 在中放入break,如果。

参考答案2

You are testing (and printing) "f" instead of "line". Be careful about that. 'f' is the file pointer, line has your data.

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

参考答案3

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

if bool == True:

sys.stdout.write(line)

cnt = cnt+1

if cnt == 3:

break

参考答案4

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

参考答案5

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:

for index, line in enumerate(file):

if "Result" in line:

print(file[index+1:index+4])

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值