怎么在for循环外 调用for循环里面的数据
如何在for循环外 调用for循环里面的数据
不过最后5行 想把key相同元素放到一起 没效果
结果是这样,第三、第四行key相同
{'192.168.90.15.1750 > 58.58.58.58.8000:': '0:10:30'}
{'192.168.90.20.1386 > 58.58.58.58.81:': '0:09:01'}
{'192.168.90.119.1891 > 220.170.79.229.80:': '0:00:00'}
{'192.168.90.119.1891 > 220.170.79.229.80:': '0:00:03'}
{'192.168.90.118.3594 > 58.58.58.58.1604:': '0:08:00'}
原始数据如下:
2012-06-11 08:28:27 192.168.90.21.1109 125.107.140.175.5938: Flags [P.]
2012-06-11 08:28:27 192.168.90.21.1109 125.107.140.175.5938: Flags [.]
2012-06-11 08:28:41 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:29:04 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:29:06 192.168.90.20.1386 58.58.58.58.81: Flags [.]
2012-06-11 08:29:11 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:29:19 192.168.90.118.3594 58.58.58.58.1604: Flags [.]
2012-06-11 08:29:22 192.168.90.21.1109 125.107.140.175.5938: Flags [P.]
2012-06-11 08:29:22 192.168.90.21.1109 125.107.140.175.5938: Flags [.]
2012-06-11 08:29:41 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:30:04 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:30:11 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:30:17 192.168.90.21.1109 125.107.140.175.5938: Flags [P.]
2012-06-11 08:30:17 192.168.90.21.1109 125.107.140.175.5938: Flags [.]
2012-06-11 08:30:41 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:30:51 192.168.90.20.1386 58.58.58.58.81: Flags [.]
2012-06-11 08:31:04 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:31:11 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:31:12 192.168.90.21.1109 125.107.140.175.5938: Flags [P.]
2012-06-11 08:31:12 192.168.90.21.1109 125.107.140.175.5938: Flags [.]
2012-06-11 08:31:19 192.168.90.118.3594 58.58.58.58.1604: Flags [.]
2012-06-11 08:31:41 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:32:04 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:32:06 192.168.90.20.1386 58.58.58.58.81: Flags [.]
2012-06-11 08:32:07 192.168.90.21.1109 125.107.140.175.5938: Flags [P.]
- Python code
#!/usr/bin/env python import datetime aDict = {} with open(r'D:\python_dev\1.txt') as fd: for line in fd: a = line.split() aday, atime, asrc, adst, aflag = a[0], a[1], a[2], a[3], a[5] aKey = asrc + ' > ' + adst if aKey in aDict: aDict[aKey].append([aday+" "+atime, aflag]) else: aDict[aKey] = [[aday+" "+atime, aflag]] delimiter = '[P.]' for key in aDict: arry = [] tmp = [] for x in aDict[key]: #print key,'x',x s = x[1] #print 's',s if s != delimiter: tmp.append(x[:1]) else: arry.append(tmp) tmp = [] arry.append(tmp) for x in arry: if len(x) < 2 : continue else: #print key for t1 in x[0]: timedict = {} for t2 in x[-1]: #print t1,t2 date1 = datetime.datetime.strptime(t1,"%Y-%m-%d %H:%M:%S") date2 = datetime.datetime.strptime(t2,"%Y-%m-%d %H:%M:%S") datesent = str(date2 -date1) #print datesent #print 'key',key if key in timedict: timedict[key].append(datesent) else: timedict[key] = datesent print timedict
不过最后5行 想把key相同元素放到一起 没效果
结果是这样,第三、第四行key相同
{'192.168.90.15.1750 > 58.58.58.58.8000:': '0:10:30'}
{'192.168.90.20.1386 > 58.58.58.58.81:': '0:09:01'}
{'192.168.90.119.1891 > 220.170.79.229.80:': '0:00:00'}
{'192.168.90.119.1891 > 220.170.79.229.80:': '0:00:03'}
{'192.168.90.118.3594 > 58.58.58.58.1604:': '0:08:00'}
原始数据如下:
2012-06-11 08:28:27 192.168.90.21.1109 125.107.140.175.5938: Flags [P.]
2012-06-11 08:28:27 192.168.90.21.1109 125.107.140.175.5938: Flags [.]
2012-06-11 08:28:41 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:29:04 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:29:06 192.168.90.20.1386 58.58.58.58.81: Flags [.]
2012-06-11 08:29:11 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:29:19 192.168.90.118.3594 58.58.58.58.1604: Flags [.]
2012-06-11 08:29:22 192.168.90.21.1109 125.107.140.175.5938: Flags [P.]
2012-06-11 08:29:22 192.168.90.21.1109 125.107.140.175.5938: Flags [.]
2012-06-11 08:29:41 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:30:04 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:30:11 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:30:17 192.168.90.21.1109 125.107.140.175.5938: Flags [P.]
2012-06-11 08:30:17 192.168.90.21.1109 125.107.140.175.5938: Flags [.]
2012-06-11 08:30:41 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:30:51 192.168.90.20.1386 58.58.58.58.81: Flags [.]
2012-06-11 08:31:04 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:31:11 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:31:12 192.168.90.21.1109 125.107.140.175.5938: Flags [P.]
2012-06-11 08:31:12 192.168.90.21.1109 125.107.140.175.5938: Flags [.]
2012-06-11 08:31:19 192.168.90.118.3594 58.58.58.58.1604: Flags [.]
2012-06-11 08:31:41 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:32:04 192.168.90.15.1750 58.58.58.58.8000: Flags [.]
2012-06-11 08:32:06 192.168.90.20.1386 58.58.58.58.81: Flags [.]
2012-06-11 08:32:07 192.168.90.21.1109 125.107.140.175.5938: Flags [P.]