我的文件包含以下行:
2015-08-11 13:31:06.609 CEST [SwitchEvent Thread] INFO o.o.t.t.internal.learning.Learning - Probabilities for Host 1: [Switch 1: 1.0]
2015-08-11 13:31:07.349 CEST [SwitchEvent Thread] INFO o.o.t.t.internal.learning.Learning - Probabilities for Host 1: [Switch 1: 0.5, Switch 2: 0.5]
我想得到它的最后一部分(例如在第1行,我想得到[Switch 1: 1.0]
)
为此,我有这种模式:(\[Switch .*\])
。
这是我正在使用的代码(的一部分):
import os
import sys
import re
data_pattern = re.compile('(\[Switch .*\])')
f = open('some_file', 'r')
for line in f:
print line
data_match = data_pattern.match(line)
print data_match.group(0)
整件事导致崩溃:
Traceback (most recent call last):
File "/home/nemo-develop/PycharmProjects/Affinity-Remote/learning_data_preparation.py", line 62, in <module>
read_file(join(directory, filename), switch_amount)
File "/home/nemo-develop/PycharmProjects/Affinity-Remote/learning_data_preparation.py", line 33, in read_file
print data_match.group(0)
AttributeError: 'NoneType' object has no attribute 'group'
2015-08-11 13:31:06.609 CEST [SwitchEvent Thread] INFO o.o.t.t.internal.learning.Learning - Probabilities for Host 1: [Switch 1: 1.0]
因此该线不匹配 我还检查了regex101。它似乎在那里工作.. Here is a link to it。
出了什么问题?
感谢
答案 0 :(得分:4)
如果模式不在行的开头,则需要使用搜索,匹配将失败,除非模式位于行的开头:
import re
data_pattern = re.compile(r'(\[Switch .*\])')
f = open('some_file', 'r')
for line in f:
print line
# CHANGE TO RE.SEARCH HERE:
data_match = data_pattern.search(line)
print data_match.group(0)