如何在Python中跳过日志文件的每一行?

2024-06-02 22:51:37 发布

您现在位置:Python中文网/ 问答频道 /正文

要使用的代码的文件包含以下数据:

<188> 2005 Sep 22 11:07:38 (FR114W-52-8f-a8) 66.190.168.225 UDP packet - Source:38.113.146.178,20841,WAN - Destination:66.190.168.225,1026,LAN [Drop] - [Inbound Default rule match]
#!^
<189> 2005 Sep 22 11:07:38 (FR114W-52-8f-a8) 66.190.168.225 Device Receive ICMP Packet - Source:192.168.1.201,[Echo Request],LAN - Destination:192.168.1.1,LAN [Receive]
#!^
<189> 2005 Sep 22 11:07:43 (FR114W-52-8f-a8) 66.190.168.225 Device Receive UDP Packet - Source:10.135.48.1,67,WAN - [Drop]

到目前为止,我掌握的代码是:

^{pr2}$

此代码的输出如下:

['#!^<188>', '2005', 'Sep', '22', '11:07:38', '(FR114W-52-8f-a8)', '66.190.168.225', 'UDP', 'packet', '-', 'Source:38.113.146.178,20841,WAN', '-', 'Destination:66.190.168.225,1026,LAN', '[Drop]', '-', '[Inbound', 'Default', 'rule', 'match]']   

('IP ', '66.190.168.225', 'Time ', '11:07:38')

['\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00#!^']   
Traceback (most recent call last):   
  File "/Users/PythonTutorials/print_line_with_match.py", line 10, in <module>   
    print ("IP ", words[6], 'Time ', words[4])   
IndexError: list index out of range   

Process finished with exit code 1

我想知道怎样才能跳过第二行来避免这个错误。我知道每一行都会导致一个错误,因为一旦它到达第二行,我就会得到一个Traceback错误。在


Tags: 代码sourcepacketmatch错误destinationsepdrop
2条回答

在for循环期间,您不必跳过一行,而是可以通过将代码修改为:

import re
import string

with open('RouterLogger.log', 'r') as file:
     for line in file:
         words = line.split()
         print words
         try:
            print ("IP ", words[6], 'Time ', words[4])
         except IndexError:
            continue

可以显式跳过第二行:

evenline = False
with open('RouterLogger.log', 'r') as file:
     for line in file:
         if not evenline:
             words = line.split()
             print words
             print ("IP ", words[6], 'Time ', words[4])
         evenline = not evenline

或者您可以(懒洋洋地)用^{}将其切片:

^{pr2}$

或者,您可以使用^{} recipes中的pairwise函数在一对行上迭代,而不是行:

with open('RouterLogger.log', 'r') as file:
     for first, second in pairwise(file):
         words = first.split()
         print words
         print ("IP ", words[6], 'Time ', words[4])

但是,你确定你的格式是“每一秒”吗?如果没有,您可能希望跳过以#开头的行:

with open('RouterLogger.log', 'r') as file:
     for line in file:
         if not line.startswith('#'):
             words = line.split()
             print words
             print ("IP ", words[6], 'Time ', words[4])

…或try每行跳过没有足够单词的行:

with open('RouterLogger.log', 'r') as file:
     for line in file:
         try:
             words = line.split()
             print words
             print ("IP ", words[6], 'Time ', words[4])
         except IndexError:
             pass

相关问题 更多 >