如何使用python读取已找到的某一行的下一行?

2024年11月15日 21:38
有4个网友回答
网友(1):

有两种情况,
1,文件比较大。
targetLine = "";
lineNo = 0;
while 1:
mLine = file.read()line();
if not mLine:
break;
lineNo += 1;
if (linecount == lineNO):
targetLine = mLine;

2, 文件比较小
targetLine = "";
mLines = file.read();
targetLine = mLines[-1];

filelineno( )
Return the line number in the current file. Before the first line has been read, returns 0. After the last line of the last file has been read, returns the line number of that line within the file.

网友(2):

readline一行一行的找,找到了再readline一次就可以了

网友(3):

import re

re.sub('\n','',string)

忽略掉换行

网友(4):

line是个什么东西?
line = "a\nb"
s = "a\n"
idx = line.find(s)
line[idx + len(s): line.find('\n', idx + len(s))]