I've been getting the error "AttributeError: 'NoneType' object has no attribute 'group'. From my understanding that's when my regex is not returning any results. I've tried it on an online regex tester and it worked though. Can someone tell me what's going on? I'm looking to have
我一直得到错误“AttributeError:'NoneType'对象没有属性'group'。根据我的理解,当我的正则表达式没有返回任何结果时。我已经在一个在线正则表达式测试器上尝试了它,但它工作正常。可以有人告诉我发生了什么事吗?我希望有
ExecutionDateTime: 04/01/2015 10:51:08.580
ExecutionDateTime:04/01/2015 10:51:08.580
ExecutionSpeed: 2.0000 ms
ExecutionSpeed:2.0000毫秒
DatabaseType: SQLite
DatabaseFileName: Local.db3
QueryName: ViewPersonEmail
import re
s = "04/01/2015 10:51:08.580 | OUTLOOK | 6 | INFO | 2.0000 ms: | SQLite | Local.db3 | Executing select query for entity type ViewPersonEmail"
def extract_data(s):
pattern = re.compile(r"""(?P<date>.*?)\s\|.+\INFO\s*\|\s*
(?P<speed>.*?)\:\s\|\s+
(?P<type>.*?)\s\|\s*
(?P<file>.*?)\s.*\Executing\s\S+\s\query for entity type\s
(?P<query>.*)""", re.VERBOSE)
match = pattern.match(s)
ExecutionDateTime = match.group("date")
ExecutionSpeed = match.group("speed")
DatabaseType = match.group("type")
DatabaseFileName = match.group("file")
QueryName = match.group("query")
return (ExecutionDateTime, ExecutionSpeed, DatabaseType, DatabaseFileName, QueryName)
print extract_data(s)
A separate question I have is what's the best way of parsing through an entire file to find strings similar to the one above to that to output into another file? I imagined I can do the same as above but have
我有一个单独的问题是解析整个文件以查找类似于上面的字符串以输出到另一个文件的最佳方法是什么?我想我可以像上面那样做,但有
log = open(text.log)
def extract_data(log)...
though I'm not sure if that's correct
虽然我不确定这是否正确
1 个解决方案
#1
0
This is a much straightforward solution without re
这是一个非常直接的解决方案,没有重新
s = "04/01/2015 10:51:08.580 | OUTLOOK | 6 | INFO | 2.0000 ms: | SQLite | Local.db3 | Executing select query for entity type ViewPersonEmail"
ExecutionDateTime, _, _ , _, ExecutionSpeed, DatabaseType, DatabaseFileName, TempQueryName = map(str.strip, s.split("|"))
QueryName = TempQueryName.split()[-1]
#1
0
This is a much straightforward solution without re
这是一个非常直接的解决方案,没有重新
s = "04/01/2015 10:51:08.580 | OUTLOOK | 6 | INFO | 2.0000 ms: | SQLite | Local.db3 | Executing select query for entity type ViewPersonEmail"
ExecutionDateTime, _, _ , _, ExecutionSpeed, DatabaseType, DatabaseFileName, TempQueryName = map(str.strip, s.split("|"))
QueryName = TempQueryName.split()[-1]