使用正则表达式解析Python中的日志文本

使用正则表达式解析Python中的日志文本,python,parsing,python-2.7,scripting,Python,Parsing,Python 2.7,Scripting,我有这样一种格式: att1="value 1" att2="value 2" att3="value 3" 比如说 level="Information" clientAddr="127.0.0.1" action="GetByName" message="Action completed" url="/customers/foo" method="GET" 我可以用正则表达式来解析这个吗?在值中,我没有任何嵌入的引号,但是我有空格,通过findall函数,你可以得到双引号中的值 &g

我有这样一种格式:

att1="value 1" att2="value 2" att3="value 3" 
比如说

level="Information" clientAddr="127.0.0.1" action="GetByName" message="Action completed" url="/customers/foo" method="GET" 

我可以用正则表达式来解析这个吗?在值中,我没有任何嵌入的引号,但是我有空格,通过findall函数,你可以得到双引号中的值

>>> import re
>>> m = 'level="Information" clientAddr="127.0.0.1" action="GetByName" message="Action completed" url="/customers/foo" method="GET"'
>>> s = re.findall(r'"([^"]*)"', m)
>>> for i in s:
...     print i
... 
Information
127.0.0.1
GetByName
Action completed
/customers/foo
GET
import xml.dom.minidom
解析的def目录(属性):
返回dict(xml.dom.minidom.parseString(“”.format(attrs)).firstChild.attributes.items()
打印解析的目录('level=“Information”clientAddr=“127.0.0.1”action=“GetByName”message=“action completed”url=“/customers/foo”method=“GET””)
{u'clientAddr':u'127.0.0.1',u'level':u'Information',u'url':u'/customers/foo',u'action':u'GetByName',u'message':u'action completed',u'method':u'GET'}

此正则表达式的可能重复项
“([^“]*)”
将完成此工作。这似乎是一个XML节点属性-我将使用XML解析器而不是正则表达式对其进行解析
import xml.dom.minidom

def parsed_dict(attrs):
    return dict(xml.dom.minidom.parseString('<node {}/>'.format(attrs)).firstChild.attributes.items())

print parsed_dict('level="Information" clientAddr="127.0.0.1" action="GetByName" message="Action completed" url="/customers/foo" method="GET"')

{u'clientAddr': u'127.0.0.1', u'level': u'Information', u'url': u'/customers/foo', u'action': u'GetByName', u'message': u'Action completed', u'method': u'GET'}