我有2个大型日志文件。我想查看一个设备是否在a而不是b,反之亦然(排除设备常见的行),这些文件看起来就像这个例子。python分析2日志文件
04/09/2010,13:11:52,Authen OK,user1,Default Group,00-24-2B-A1-08-88,29,10.1.1.1,(Default),,,, ,13,EAP-TLS ,, device1,
04/19/2010,15:35:24,Authen OK,user2,Default Group,00-24-2B-A1-05-EA,29,10.1.1.2 ,(默认),,,,,, 13,EAP-TLS ,, device2,
04/09/2010,13:11:52,Authen OK,user3,Default Group,00-24-2B-A1-08 -88,29,10.1.1.3,(默认),,,,,, 13,EAP-TLS ,, device3,
04/19/2010,15:35:24,Authen OK,user4,Default Group,00 -24-2B-A1-05-EA,29,10.1.1.4,(默认),,,,,, 13,EAP-TLS ,, device4,
重申,我需要device(field [ 2])和IP(f ield [7])对于日志文件中的每个设备a但不是b,并且在b中但不是
以下是我迄今为止所做的,但似乎有点笨拙并且非常慢(每个文件都有约400K行)。我交叉两次。任何人都可以提出效率吗?也许我正在使用错误的逻辑?
chst={}
chbs={}
for i,line in enumerate(open('chst.txt').readlines()):
line=line.split(',')
chst[line[-2]+','+str(i)]=','.join(line)
for i,line in enumerate(open('chbs.txt').readlines()):
line=line.split(',')
chbs[line[-2]+','+str(i)]='.'.join(line)
print "these lines are in CHST but not in CHBS"
for a in chst:
if a.split(',')[0] not in str(chbs.values()):
line=chst[a].split(',')
print line[-2], line[7]
print "\nthese lines are in CHBS but not in CHST"
for a in chbs:
if a.split(',')[0] not in str(chst.values()):
line=chbs[a].split(',')
print line[-2], line[7]
自Python 2.6以来,sets模块已被弃用。从2.6开始,set和frozensets确实是内置的。 – 2010-08-11 08:52:38
哎呀,后台团队*已经很忙!固定。 – katrielalex 2010-08-11 08:58:15
我也很确定只是调用项目将无法正常工作(并且与dict视图无关) - 您必须在该字典上调用viewitems,从2.7开始支持。 items方法只返回键/值对的列表,而对于列表,不支持^运算符,而viewitems返回dict_ietms类型的实际视图。 – 2010-08-11 09:04:55