2015-05-10 54 views
1

文件的代碼中使用包含此位的數據:如何從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錯誤。

回答

4

您可以跳過明確每一個下聯:

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切它:

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 recipes

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 
+0

感到驚訝的是,你還沒有提出這個 - open('log.txt').readlines()[:: 2]'。我缺少什麼優勢案例? – fixxxer

+0

@fixxxer:你不想一次將整個文件讀入內存,或者不想泄漏文件描述符的情況? – abarnert

+0

1. *燈泡*但文件有多大? 2.怎麼這樣? – fixxxer

3

而不是在你的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 
相關問題