0

我正在努力锻炼最好只打印编号的行。代码只是部分完成,因为我对正则表达式还是新手,所以可能没有使用正确的方法或语法。单独的 re.matches 工作正常,当我将它们结合起来时,我会得到不需要的结果:

示例字符串:

file = '''
title|Head1|Head2|Head3|head4 
----|------|-----|-----|
1|1150976|0|25300992|bfa92720/bfa924f8|su|(None, None)
2|1150976|0|25300992|bfa92720/bfa924f8|su|(None, None)
3|1150976|0|25300992|bfa92720/bfa924f8|su|(None, None)
4|1150976|0|25300992|bfa92720/bfa924f8|su|(None, None)
5|1150976|0|25300992|bfa92720/bfa924f8|su|(None, None)
All|processes:|MemAlloc|=|408125440|(None, None)|0.0.0.0
|(None, None)
0.0.0.0 ,text
''' 
import re
for line in file:
    pat= re.match('(^[A-Z][a-z])|(^--.+)',line) # or use re.match('^[0-9]',line) and match pat != None
    patIP = re.match ('^{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}',line)#
    if patIP == None  or pat == None:
        print(line)

我坚持只打印编号行的逻辑,.. 我可能完全关闭了.. 请记住,我不想打印 0.0.0.0(IP 地址) 行。

所需的输出:

1|1150976|0|25300992|bfa92720/bfa924f8|su|(None, None)
2|1150976|0|25300992|bfa92720/bfa924f8|su|(None, None)
3|1150976|0|25300992|bfa92720/bfa924f8|su|(None, None)
4|1150976|0|25300992|bfa92720/bfa924f8|su|(None, None)
5|1150976|0|25300992|bfa92720/bfa924f8|su|(None, None)
4

2 回答 2

1
import io
import re
import sys

file = io.StringIO('''
title|Head1|Head2|Head3|head4 
----|------|-----|-----|
1|1150976|0|25300992|bfa92720/bfa924f8|su|(None, None)
2|1150976|0|25300992|bfa92720/bfa924f8|su|(None, None)
3|1150976|0|25300992|bfa92720/bfa924f8|su|(None, None)
4|1150976|0|25300992|bfa92720/bfa924f8|su|(None, None)
5|1150976|0|25300992|bfa92720/bfa924f8|su|(None, None)
All|processes:|MemAlloc|=|408125440|(None, None)|0.0.0.0
|(None, None)
0.0.0.0 ,text
''')

sys.stdout.writelines(line for line in file if re.match('\d+\|', line))
于 2013-06-20T16:53:19.820 回答
0

你可以试试这个:

import re

file = '''
title|Head1|Head2|Head3|head4 
----|------|-----|-----|
1|1150976|0|25300992|bfa92720/bfa924f8|su|(None, None)
2|1150976|0|25300992|bfa92720/bfa924f8|su|(None, None)
3|1150976|0|25300992|bfa92720/bfa924f8|su|(None, None)
4|1150976|0|25300992|bfa92720/bfa924f8|su|(None, None)
5|1150976|0|25300992|bfa92720/bfa924f8|su|(None, None)
All|processes:|MemAlloc|=|408125440|(None, None)|10.93.103.73|(None, None)
0.0.0.0 ,text
''' 

matches = re.findall(r'^\d+\|.*$', file, re.MULTILINE)
for match in matches:
    print match

使用多行模式时,^代表$行尾

于 2013-06-20T17:00:00.313 回答