从文件中只读取特定行(基于匹配文本)的最佳方式是什么?这是我现在在做什么:一个从文件中读取特定行
match_txt = "lhcb"
for inFile in os.listdir('.'):
readFile = open(inFile, 'r')
lines = readFile.readlines()
readFile.close()
for line in lines:
if line.find(match_txt)==0:
#< do stuff here >
即我读台词,只能用“LHCb的”在里面,从本目录中的一个的所有文件。这是做这件事的最好方法吗?可以在不首先将整个文件加载到内存中的情况下完成吗?
可能重复)](http://stackoverflow.com/questions/2081836/reading-specific-lines-only-python) – 2011-02-14 03:39:26
最好的方法?从搜索开始。 http://stackoverflow.com/questions/2081836/reading-specific-lines-only-python – 2011-02-14 03:39:43