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

使用line.find()已经找到了文本中的某一行,现在想要接着找到紧跟其后的下一行,该如何编写代码呢?或使用什么命令或语句?请高手赐教。

第1个回答  2011-01-01
有两种情况,
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个回答  2011-01-07
readline一行一行的找,找到了再readline一次就可以了
第3个回答  2011-01-06
import re

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

忽略掉换行
第4个回答  2010-12-31
line是个什么东西?
line = "a\nb"
s = "a\n"
idx = line.find(s)
line[idx + len(s): line.find('\n', idx + len(s))]本回答被网友采纳
相似回答