我的任务是下一步。我有一份档案。我需要从文件中获得确切的字符串,再加上指定字符串前面的五个字符串。我试着这样做:
import re
import glob
index = 0
ArrayListStringIndex = []
for filename in glob.glob('syslog'):
file = open ((filename), "r")
for SearchPrase in file:
if re.search ((": New USB device found"), SearchPrase):
ArrayListStringIndex.append(index)
index = index + 1
但我不知道如何将我从文件中获得的字符串数列表(ArrayListStringIndex=[])与实际字符串连接起来,之前分别得到了五个Sting
提前谢谢你的帮助
你可以试试
这将打印所有精确的匹配行,前面有5行
可以使用长度为5的^{} 作为缓存。您只需在迭代时附加每个字符串,deque负责限制大小,根据需要从前面弹出项目。例如:
输出:
这也可以轻松地处理早期事件,例如使用
target = '3'
:相关问题 更多 >
编程相关推荐