match.groupdict() into a single dict

Ganesh Pal ganesh1pal at
Wed Apr 19 09:26:41 EDT 2017

Hello friends,

I am learning regex and trying to use this to my scripts  I need some
suggestion on the below code.   I need to match all lines of a file that
have a specific pattern
and return them as a dictionary.

Sample line:

'NODE=ADAM-11: | TIME=2017-04-14T05:27:16-07:00 |  COND=Some lovely message
| MSG=attempt to record { addr=1,0,17080320:8192 action=xxhello-hell
o owner=1:0070:001a::HEAD }, but history information has a different owner:
owner: 1:0064:0005::HEAD, actions (new->old): { hello-hello
 * 1, none, none, hello-hello * 1, none, none, hello-hello * 1, none, none,
hello-hello * 1, none, none, hello-hello * 1, none, hello-h
ello * 1, none } bh hello_cookie: 8:hello-only bhv | LINSNAP=None | MAP=none

with open("/tmp/2.repo","r") as f:
     for line in f:
         result ='MSG=attempt to record(.*)LINSNAP', line)
         if result:
            for pattern in [ r'(?P<Block>(\d+,\d+,\d+:\d+))',


                regex = re.compile(pattern)
                match =
                print '  ', match.groupdict()

sample o/p:

  {'Block': '1,0,17080320:8192'}
   {'p_owner': '1:0070:001a::HEAD'}
   {'a_owner': 'owner: 1:0064:0005::HEAD'}


1. I was expecting a single dictionary with all matches every a line ,
something like below

   {'Block': '1,0,17080320:8192', 'p_owner': '1:0070:001a::HEAD','a_owner':
'owner: 1:0064:0005::HEAD'}

   (a)  I am thinking to  add these element  {'Block': '1,0,17080320:8192'}
, {'p_owner': '1:0070:001a::HEAD'} ... etc to new dictionary

   (b) or some better regex may be the for loop is not needed  and complied
pattern can be better.

I am a Linux user and on Python 2.7 , Thanks in advance :)

More information about the Python-list mailing list