adglopez
adglopez

Reputation: 66

Parse log text in Python using Regex

I've a format like this:

att1="value 1" att2="value 2" att3="value 3" 

for example

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

Can I use regex to parse this? inside the values I won't have any embedded quotes but I'll have spaces

Upvotes: 0

Views: 94

Answers (2)

Alex P.
Alex P.

Reputation: 31666

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'}

Upvotes: 1

Avinash Raj
Avinash Raj

Reputation: 174696

Through findall function , you could get the values inside double quotes.

>>> 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

Upvotes: 1

Related Questions