[英]How to skip every second line from a log file in python?
The file for the code to use contains this bit of data: 要使用的代码文件包含以下数据:
<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]
The code I have so far is: 到目前为止,我的代码是:
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])
The output for this code is this: 此代码的输出是这样的:
['#!^<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
I would like to know how can I skip every second line to avoid this error. 我想知道如何跳过第二行以避免此错误。 I know every other line causes an error because I get a
Traceback
error as soon as it hits the second line. 我知道每隔一行会导致错误,因为一旦碰到第二行,我就会得到
Traceback
错误。
You can skip every second line explicitly: 您可以显式跳过第二行:
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
Or you can (lazily) slice it with islice
: 或者,您可以(懒惰地)使用
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])
Or you can iterate over pairs of lines instead of lines, using the pairwise
function in the itertools
recipes : 或者你也可以遍历对线而不是线,采用
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])
However, are you absolutely sure that your format is "every second line"? 但是,您绝对确定您的格式是“第二行”吗? If not, maybe you want to skip lines that start with
#
: 如果没有,也许您想跳过以
#
开头的行:
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])
… or try
every line and skip the ones without enough words: …或
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
Rather than skipping a line during your for loop you can handle the exception by modifying your code to: 您可以通过将代码修改为以下内容来处理异常,而不是在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.