簡體   English   中英

如何從python中的日志文件中跳過每一行?

[英]How to skip every second line from a log file in 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]

到目前為止,我的代碼是:

import re
import string

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

此代碼的輸出是這樣的:

['#!^<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錯誤。

您可以顯式跳過第二行:

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

或者,您可以(懶惰地)使用islice對其進行islice

with open('RouterLogger.log', 'r') as file:
     for line in itertools.islice(file, 0, None, 2):
         words = line.split()
         print words
         print ("IP ", words[6], 'Time ', words[4])

或者你也可以遍歷對線而不是線,采用pairwise的功能在itertools食譜

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

您可以通過將代碼修改為以下內容來處理異常,而不是在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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM