【发布时间】:2016-04-27 05:13:26
【问题描述】:
我有一个包含多个命令(以 ; 结尾)及其输出(直到 END)的大型日志,如下所示:
<blabla;
foo
...
...
END
<xyz;
...
...
END
--and so on
要求是具有单独的文件,其命令名称如
blabla
xyz
并且在每个文件中应该是它们各自的输出。
到目前为止我有:
def generateDicts(log_fh):
currentDict = {}
for line in log_fh:
if line.endswith(";"):
if line.endswith("END"):
yield currentDict
currentDict = {""}
else:
currentDict["text"] += line
yield currentDict
with open("logfile.txt") as f:
print list(generateDicts(f))
请帮忙。
【问题讨论】:
-
1) 您的问题是什么? 2)你的解决方案有什么不足?它会打印错误吗?它是否无法正确执行?