python字典嵌套字典实例
日志文件样式:
2018-06-27 09:07:37 Postman[INFO]: [1530061656f8lda-7M5E9] from: <lilh@test.com><router>, to: <xiey@test.com,zhengj@test.com>, size: 22018
2018-06-27 09:07:37 Postman[INFO]: [1530061656f8lda-7M5E9] system rule send: pass filter
2018-06-27 09:07:37 Postman[INFO]: [1530061656f8lda-7M5E9] system rule recv: pass filter
2018-06-27 09:07:37 Postman[INFO]: [1530061656f8lda-7M5E9] user rule [xiey@test.com]: pass filter
2018-06-27 09:07:37 Postman[INFO]: [1530061656f8lda-7M5E9] user rule [xiey@test.com] result : True [] False
2018-06-27 09:07:37 Postman[INFO]: [1530061656f8lda-7M5E9] save file to: INBOX (xiey@test.com)
2018-06-27 09:07:37 Postman[INFO]: [1530061656f8lda-7M5E9] deliver mail to : xiey@test.com
2018-06-27 09:07:37 Postman[INFO]: [1530061656f8lda-7M5E9] user rule [zhengj@test.com]: pass filter
2018-06-27 09:07:37 Postman[INFO]: [1530061656f8lda-7M5E9] user rule [zhengj@test.com] result : True [] False
2018-06-27 09:07:37 Postman[INFO]: [1530061656f8lda-7M5E9] save file to: INBOX (zhengj@test.com)
2018-06-27 09:07:37 Postman[INFO]: [1530061656f8lda-7M5E9] deliver mail to : zhengj@test.com
2018-06-27 09:07:37 Postman[INFO]: [1530061656f8lda-7M5E9] add reply task: xiey@test.com (AhKz9)
2018-06-27 09:07:37 Postman[INFO]: wx_notify_worker start
2018-06-27 09:07:37 Postman[INFO]: wx_notify_worker finish
2018-06-27 09:07:37 Postman[INFO]: program quit
2018-06-27 09:07:45 Postman[INFO]: program start
2018-06-27 09:07:45 Postman[INFO]: [1530061664nXQwH-r98pc] from: <gxm@domain.com><router>, to: <li@ceshi.com>, size: 14371
2018-06-27 09:07:45 Postman[INFO]: [1530061664nXQwH-r98pc] system rule recv: pass filter
2018-06-27 09:07:45 Postman[INFO]: [1530061664nXQwH-r98pc] user rule [li@ceshi.com]: pass filter
2018-06-27 09:07:45 Postman[INFO]: [1530061664nXQwH-r98pc] user rule [li@ceshi.com] result : True [] False
2018-06-27 09:07:45 Postman[INFO]: [1530061664nXQwH-r98pc] 192.168.10.20 is trustip
2018-06-27 09:07:45 Postman[INFO]: [1530061664nXQwH-r98pc] save file to: INBOX (li@ceshi.com)
2018-06-27 09:07:45 Postman[INFO]: [1530061664nXQwH-r98pc] deliver mail to : li@ceshi.com
2018-06-27 09:07:45 Postman[INFO]: [1530061664nXQwH-r98pc] add reply task: li@ceshi.com (YV5Lm)
2018-06-27 09:07:45 Postman[INFO]: wx_notify_worker start
2018-06-27 09:07:45 Postman[INFO]: wx_notify_worker finish
2018-06-27 09:07:45 Postman[INFO]: program quit
脚本代码:
#!/usr/bin/python
# coding: utf-8
import os
import re
import pprint
d = {} #定义一个空字典
with open("/root/postman.log") as f: #打开日志文件,并赋值给f,使用with表示完成后会自动关闭
for line in f: #循环读取每一行
m = re.search(r'\[([0-9A-Za-z-]{21})\].*size: ([0-9]+)$', line.strip()) #按照正则表达式查找id和size,strip()去除收尾空格
if m is not None: #假如没有返回值,即没有匹配到,然后跳出这个if语句块
id, size = m.groups() #返回一个元组,索引0为上面第一个正则表达式匹配到的ID值,索引1为上面第二个正则表达式匹配到的size值(m.groups() 其实等于 (m.group(1), m.group(2)))
if id not in d: #如果id不在字典d里面,然后初始化(recp这个初始化为列表),下一次循环的时候,同一个id就不会执行if语句块(即每个ID第一次都会初始化字典的值)
d[id] = {
'size': 0,
'recp': []
}
d[id]['size'] = int(size) #如果id存在字典中,将m.group(1)赋值给字典中size,并转换成×××
m = re.search(r'\[([0-9A-Za-z-]{21})\] save file to:.*\(([^\(\)]+)\)$', line.strip()) #按照正则表达式查找id和收件人,strip()去除收尾空格
if m is not None: #假如没有返回值,即没有匹配到,然后跳出这个if语句块
id, recp = m.groups() #返回一个元组,索引0为上面第一个正则表达式匹配到的ID值,索引1为上面第二个正则表达式匹配到的收件人值(m.groups() 其实等于 (m.group(1), m.group(2)))
if id not in d: #如果id不在字典d里面,然后初始化(recp这个初始化为列表),下一次循环的时候,同一个id就不会执行if语句块(即每个ID第一次都会初始化字典的值)
d[id] = {
'size': 0,
'recp': []
}
d[id]['recp'].append(recp) #如果id存在字典中,将m.group(2)添加到字典中recp列表中
l = [] #定义一个空列表
for id, e in d.items(): #循环遍历字典d,得到id和e,其中e是子字典。
total_size = e['size'] * len(e['recp']) / (1024.0 ** 2) #所以同一个id的总容量=大小*recp的长度即数量,再由B换算成M
l.append((id, len(e['recp']), total_size)) #将这三个值(id、收件人数量、总容量),作为一个元素添加到列表
print("\n")
print(">>>>>>>>>>>>>>>统计每隔ID产生的收件人数量和总容量(TOP20)>>>>>>>>>>>>>>>")
#l.sort(key=lambda x: x[2], reverse=True) #给l列表排序下,以列表索引值2为排序条件
def key_func(x):
return x[2]
l.sort(key=key_func, reverse=True)
'''
函数作为参数
key_func(l[0]) -> 123
key_func(l[1]) -> 456
函数作为返回值
'''
for id, recp_num, total_size in l[:20]: #取前排序后的前20个元素,然后循环并按照下面的格式打印出来
print('邮件id为{},收件人数量为{}个,总大小为{}M'.fORMat(id,recp_num,total_size))
字典样式:
{'1530028804NYIxH-Si3rT': {'recp': [], 'size': 165241}, '1530028863UJOjD-1AxwS': {'recp': [], 'size': 3007}, '1530028818QBDbP-KLgft': {'recp': [], 'size': 85981}}
运行结果:
相关文章