2015-05-10 55 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 
相关问题