有沒有辦法打印第一個字符串,只有當它發現字符串?例如:只打印Python文本文件中第一個找到的字符串
文本文件:
date....
author:...
this is a list:
there a string 1 here
and there a string 2 there
but also have string 3 here
don't have string 4 there
代碼:
for line in open(os.path.join(dirname, filename), "r").readlines():
if line.find('string') != -1:
print "found ", line
印刷:
found there a string 1 here
你能解釋更多的?它不清楚,第一個字符串? – Hackaholic 2014-12-03 10:10:54