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

2024-11-15 21:38:45
推荐回答(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))]