从事此工作已经有一段时间了,我在此站点上未找到任何示例,也未找到任何其他相关的示例。我有一个列表,我想做的很简单。我只需要搜索该列表以找到关键字“ Buffer Log”。找到该关键字后,我需要打印该行的每一行,直到列表末尾。任何方向都将不胜感激。看来我已经很接近了。
logfile = open('logs.txt', 'r')
readlog = logfile.readlines()
logfile.close()
lines = []
for line in readlog:
lines.append(line)
for x in lines:
if "Log Buffer" in x:
z =lines.index(x)
print(lines[z:]
Run Code Online (Sandbox Code Playgroud)
首先,代码:
lines = []
for line in readlog:
lines.append(line)
Run Code Online (Sandbox Code Playgroud)
不需要,因为readlog已经是列表。您可以尝试以下方法:
found = False
for line in readlog: # because this is already a list
if "Log Buffer" in line:
found = True # set our logic to start printing
if found:
print(line)
Run Code Online (Sandbox Code Playgroud)