我是新的蟒蛇...我想读取一个python日志文件,并作出字典。记录器如何处理?Python记录器到字典
1
A
回答
1
读取python日志文件并创建字典。记录器如何处理?
它不会。
logging
写入日志。
file
读取日志。
你在问什么?
首先,搜索[python]的日志解析:https://stackoverflow.com/search?q=%5Bpython%5D+log+parsing
二,请张贴一些示例代码。
1
正如其他评论者所说,您不想使用logging
来读取文件,而是使用file
。这里有一个写日志文件的例子,然后读回来。
#!/usr/bin/env python
# logger.py -- will write "time:debug:A:1" "time:debug:B:2" "time:debug:A:3" etc. log entries to a file
import logging, random
logging.basicConfig(filename='logfile.log',level=logging.DEBUG)
for i in range(1,100): logging.debug("%s:%d" % (random.choice(["a", "b"]), i))
# logfile.log now contains --
# 100.1:debug:A:1
# 100.5:debug:B:2
# 100.8:debug:B:3
# 101.3:debug:A:4
# ....
# 130.3:debug:B:100
#!/usr/bin/env/python
# reader.py -- will read aformentioned log files and sum up the keys
handle = file.open('logfile.log', 'r')
sums = {}
for line in handle.readlines():
time, debug, key, value = line.split(':')
if not key in sums: sums[key] = 0
sums[key] += value
print sums
# will output --
# "{'a': 50, 'b': 50}"
相关问题
- 1. 词典到Python的字典
- 2. Python字典到CSV
- 3. Python记录在混帐庆典
- 4. C#将一个记录从一个字典复制到另一个字典
- 5. 迭代器在Python字典
- 6. 如何通过字典键值促进字典记录过滤?
- 7. 是字典来记录一个关键
- 8. jquery字典显示前3条记录
- 9. 如何从elasticsearch-py记录unicode字典?
- 10. 在SwiftyJSON阵列删除字典记录
- 11. Python - 追加到字典
- 12. python文件到字典
- 13. Python字典到csv excel
- 14. python字典插入到mysql
- 15. Python字典到URL参数
- 16. python字典到csv文件
- 17. Python json字典到数组
- 18. 添加到Python字典
- 19. Excel表格到python字典
- 20. Python列表到字典
- 21. Python记录打字时间
- 22. python中的记录器链
- 23. 更换的Python记录器
- 24. 命名Python记录器
- 25. 字典在python字典
- 26. python字典 - 列表字典
- 27. Python记录器调试:没有处理程序可以找到记录器“__main__”
- 28. 循环登录凭证 - Python字典
- 29. Python记录器,打印子记录器%(姓名)没有父名
- 30. Python根记录器隐藏后代记录器级别
什么样的字典?什么样的日志文件?请显示您的输入样本和您的预期输出。 – 2010-11-02 09:39:04
...... W时的T? – 2010-11-02 09:39:08
可能重复[什么是解析日志文件的最佳工具?](http://stackoverflow.com/questions/1994355/whats-the-best-tool-to-parse-log-files) – 2010-11-02 09:58:46