Python-列表问题(多个列表?)

Python-列表问题(多个列表?),python,list,Python,List,下面是我试图写的脚本的一部分。脚本打开我的iptables日志,日志中的每一行都包含下面示例中的详细信息 #example of a single line #Mar 9 14:57:51 machine kernel: [23780.638839] IPTABLES Denied UDP: IN=p21p1 OUT= MAC=ff:ff:ff:ff:ff:ff:00:00:00:00:00:00:00:00 SRC=10.100.1.4 DST=10.100.1.63 LEN=78 TOS=

下面是我试图写的脚本的一部分。脚本打开我的iptables日志,日志中的每一行都包含下面示例中的详细信息

#example of a single line #Mar 9 14:57:51 machine kernel: [23780.638839] IPTABLES Denied UDP: IN=p21p1 OUT= MAC=ff:ff:ff:ff:ff:ff:00:00:00:00:00:00:00:00 SRC=10.100.1.4 DST=10.100.1.63 LEN=78 TOS=0x00 PREC=0x00 TTL=128 ID=10898 PROTO=UDP$ # Read file in a line at a time for line in iptables_log.readlines(): #find time based on 4 letters, 2 spaces, up to 2 numbers, 1 space, then standard 10:10:10 time format time = re.findall('(^\w{1,4}\s\s\d{1,2}\s\d\d:\d\d:\d\d)', line) #mac lookup mac = re.findall('MAC=(?:\w\w:\w\w:\w\w:\w\w\:\w\w:\w\w:\w\w:\w\w:\w\w:\w\w:\w\w:\w\w:\w\w:\w\w)', line) #source port src = re.findall('SRC=(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})', line) #destination port dst = re.findall('DST=(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})', line) #protocol proto = re.findall('PROTO=(?:\w{3,4})', line) #sourceport sourceport = re.findall('SPT=(?:\w{1,5})', line) #destport destport = re.findall('DPT=(?:\w{1,5})', line) print time, mac, src, dst, proto, sourceport, destport print '======================================================' #单行示例 #3月9日14:57:51机器内核:[23780.638839]IPTABLES被拒绝UDP:IN=p21p1 OUT=MAC=ff:ff:ff:ff:00:00:00:00:00:00:00:00 SRC=10.100.1.4 DST=10.100.1.63 LEN=78 TOS=0x00 PREC=0x00 TTL=128 ID=10898 PROTO=UDP$ #一次读取一行中的文件 对于iptables_log.readlines()中的行: #根据4个字母、2个空格、最多2个数字、1个空格和标准10:10:10时间格式查找时间 time=re.findall(“(^\w{1,4}\s\s\d{1,2}\s\d\d\d:\d\d:\d\d)”,第行) #mac查找 mac=re.findall('mac=(?:\w\w:\w\w:\w\w\:\w\w:\w\w:\w\w:\w\w:\w\w:\w\w:\w\w:\w\w:\w\w:\w\w:\w\w)行) #源端口 src=re.findall('src=(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})行) #目的港 dst=re.findall('dst=(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})行) #协议 proto=re.findall('proto=(?:\w{3,4}'),第行) #源端口 sourceport=re.findall('SPT=(?:\w{1,5}'),第行) #目的港 destport=re.findall('DPT=(?:\w{1,5}'),第行) 打印时间、mac、src、dst、proto、源端口、目的端口 打印“=================================================================================” 我试图让脚本只打印我想要的项目,但是当脚本输出时,它看起来像这样,看起来像是一个列表。我希望打印时不带[]''。从网上看,似乎每个变量(时间、mac、src等)本身就是一个列表。我不知道如何把它们结合起来。我已经看到了join的引用,但不确定如何在这个示例中使用它。有人能帮忙吗

['Mar 9 14:57:51'] ['MAC=ff:ff:ff:ff:ff:ff:00:00:00:00:00:00:00:00'] ['SRC=10.100.1.4'] ['DST=10.100.1.63'] ['PROTO=UDP'] ['SPT=137'] ['DPT=137'] [Mar 9 14:57:51'][MAC=ff:ff:ff:ff:ff:00:00:00:00'][SRC=10.100.1.4'][DST=10.100.1.63'][PROTO=UDP'][SPT=137'][DPT=137']
为什么不打开你的清单呢

>>> time = [0]
>>> [time] = time
>>> time
0

为什么不打开你的清单呢

>>> time = [0]
>>> [time] = time
>>> time
0
你可以这样做:

foo = re.findall(…)[0]
如果您只期望一个结果,那么对于任何re.findall,您可以执行以下操作:

foo = re.findall(…)[0]

如果您只希望得到一个结果,那么对于任何re.findall,re.findall将返回一个列表

def findall(pattern, string, flags=0):
    """Return a list of all non-overlapping matches in the string.

    If one or more groups are present in the pattern, return a
    list of groups; this will be a list of tuples if the pattern
    has more than one group.

    Empty matches are included in the result."""
    return _compile(pattern, flags).findall(string)
我会用搜索代替

>>> import re
>>> st = '''Mar  9 14:57:51 machine kernel: [23780.638839] IPTABLES Denied UDP: IN=p21p1 OUT= MAC=ff:ff:ff:ff:ff:ff:00:00:00:00:00:00:00:00 SRC=10.100.1.4 DST=10.100.1.63 LEN=78 TOS=0x00 PREC=0x00 TTL=128 ID=10898 PROTO=UDP$'''
>>> x = re.search('(^\w{1,4}\s\s\d{1,2}\s\d\d:\d\d:\d\d)',st)
>>> x.group(0)
'Mar  9 14:57:51'

(source=)

re.findall返回一个列表

def findall(pattern, string, flags=0):
    """Return a list of all non-overlapping matches in the string.

    If one or more groups are present in the pattern, return a
    list of groups; this will be a list of tuples if the pattern
    has more than one group.

    Empty matches are included in the result."""
    return _compile(pattern, flags).findall(string)
我会用搜索代替

>>> import re
>>> st = '''Mar  9 14:57:51 machine kernel: [23780.638839] IPTABLES Denied UDP: IN=p21p1 OUT= MAC=ff:ff:ff:ff:ff:ff:00:00:00:00:00:00:00:00 SRC=10.100.1.4 DST=10.100.1.63 LEN=78 TOS=0x00 PREC=0x00 TTL=128 ID=10898 PROTO=UDP$'''
>>> x = re.search('(^\w{1,4}\s\s\d{1,2}\s\d\d:\d\d:\d\d)',st)
>>> x.group(0)
'Mar  9 14:57:51'

(source=)

re.findall返回匹配项列表。在您的例子中,您得到的列表只有一个值。如果总是这样,那么@x539 answer将获得列表中的第一项。

re.findall返回匹配项列表。在您的例子中,您得到的列表只有一个值。如果总是这样,那么@x539 answer将获得列表中的第一项。

我建议对整行使用单个regexp,并使用命名组,例如:

>>> r = re.compile(r'^(?P<date>\w{1,4}\s\d{1,2}\s\d\d\:\d\d\:\d\d) (?P<hostname>\w+) kernel: (\[[0-9]+.[0-9]+\]) IN=(?P<ifacein>[a-z0-9]*) OUT=(?P<ifaceout>[a-z0-9]*) MAC=(?P<mac>[a-f0-9:]+) SRC=(?P<src>[\w.:]+) DST=(?P<dst>[\w:.]+) LEN=(?P<len>[0-9]+) TOS=0x(?P<tos>[0-9a-f]+) PREC=0x(?P<prec>[0-9a-f]+) TTL=(?P<ttl>[0-9]+) ID=(?P<id>[0-9]+) PROTO=(?P<proto>[A-Z]+) SPT=(?P<spt>[0-9]+) DPT=(?P<dpt>[0-9]+) LEN=(?P<len2>[0-9]+)')
>>> d = r.match(line).groupdict()
>>> d['dst']
'255.255.255.255'
>>> d['proto']
'UDP'
>>> d['dpt']
'17500'

我建议对整行使用单个regexp,并使用命名组,例如:

>>> r = re.compile(r'^(?P<date>\w{1,4}\s\d{1,2}\s\d\d\:\d\d\:\d\d) (?P<hostname>\w+) kernel: (\[[0-9]+.[0-9]+\]) IN=(?P<ifacein>[a-z0-9]*) OUT=(?P<ifaceout>[a-z0-9]*) MAC=(?P<mac>[a-f0-9:]+) SRC=(?P<src>[\w.:]+) DST=(?P<dst>[\w:.]+) LEN=(?P<len>[0-9]+) TOS=0x(?P<tos>[0-9a-f]+) PREC=0x(?P<prec>[0-9a-f]+) TTL=(?P<ttl>[0-9]+) ID=(?P<id>[0-9]+) PROTO=(?P<proto>[A-Z]+) SPT=(?P<spt>[0-9]+) DPT=(?P<dpt>[0-9]+) LEN=(?P<len2>[0-9]+)')
>>> d = r.match(line).groupdict()
>>> d['dst']
'255.255.255.255'
>>> d['proto']
'UDP'
>>> d['dpt']
'17500'

为什么不使用一个正则表达式呢?为什么不使用一个正则表达式呢?如果字符串中没有与模式匹配的位置,则返回
None
。因此,在访问
MatchObject
之前,您应该检查您的结果是否不是
None
。如果字符串中没有与模式匹配的位置,则返回
None
。因此,在访问
MatchObject
之前,您应该检查您的结果是否不是
None
。列表解包工作与元组解包类似:>>>元组=(0,1)>>(x,y)=元组>>>x 0>>>y 1元组解包更常用(因为您经常从具有多个结果的函数/方法返回元组).List解包与元组解包类似:>>>元组=(0,1)>>>>(x,y)=元组>>>x 0>>>y元组解包的使用率更高(因为您经常从具有多个结果的函数/方法返回元组)。