Reputation: 185
I need to transform a list of strings similar to this:
"ABOC000 RECORD 0 Msg-type\=0220 Bit-map\=3450G83H403894JH Xbit-map\=0000000000000010 Proc code\=312000 Tran amt\=000000000000 Tran datetime\=0613064645 Trace nbr\=000000 Local time\=02:46:37 Local date\=06/13 Exp date\=24/02 Sett date\=06/13 Merchant\=6011 Pos entry\=051 Card seq no\=000 Acqr inst id\=2349823498 Cord \=23049583049583405983045983405983405900 Retr ref\=111111111111 Resp code\=00 Crd acpt trmid\=CS61252 Crd acpt id\=ISPA/PULSE Crd acpt loc\=000 8TH AVENUE BOREALIS XXUS Name\=MERCHANT NAME Tran curr\=840 Natl cond code\=1010000002U Reason codes\=004 Rsn code map\=40 Advice reason\=31 Ddsi data len\=022 Ddsi data map\=B2 Pseudo term\=070792 Acqr netid\=PUL Processor id\=INT789 Proc flags\= Info text\=NI24PS20ID16 03 "
into a list of dicts with key/values.
This is using python 3.7 -- I've gone down the list comprehension and regex paths, but have not found a workable solution yet. The difficult lies around:
A short example of what I'm aiming to end up with:
[{"RECORD":"0", "Msg-type":"0220", "Bit-map":"3450G83H403894JH", "Xbit-map":"0000000000000010", "Proc code":"312000" ... }]
Upvotes: 0
Views: 27
Reputation: 36249
Assuming that the values don't contain any whitespace (otherwise it would be hard to distinguish what part belongs to the previous value or to the next key), and stripping off the beginning of the string (not sure how the {'RECORD': '0'}
fits in the picture), you can use re.findall
with the following regex:
s = r"Msg-type\=0220 Bit-map\=3450G83H403894JH Xbit-map\=0000000000000010 Proc code\=312000 Tran amt\=000000000000 Tran datetime\=0613064645 Trace nbr\=000000 Local time\=02:46:37 Local date\=06/13 Exp date\=24/02 Sett date\=06/13 Merchant\=6011 Pos entry\=051 Card seq no\=000 Acqr inst id\=2349823498 Cord \=23049583049583405983045983405983405900 Retr ref\=111111111111 Resp code\=00 Crd acpt trmid\=CS61252 Crd acpt id\=ISPA/PULSE Crd acpt loc\=000 8TH AVENUE BOREALIS XXUS Name\=MERCHANT NAME Tran curr\=840 Natl cond code\=1010000002U Reason codes\=004 Rsn code map\=40 Advice reason\=31 Ddsi data len\=022 Ddsi data map\=B2 Pseudo term\=070792 Acqr netid\=PUL Processor id\=INT789 Proc flags\= Info text\=NI24PS20ID16 03 "
d = dict(re.findall(r'([A-Za-z][A-Za-z \-]*)\\=([^\s]+)', s))
Which gives:
{'Acqr inst id': '2349823498',
'Acqr netid': 'PUL',
'Advice reason': '31',
'Bit-map': '3450G83H403894JH',
'Card seq no': '000',
'Cord ': '23049583049583405983045983405983405900',
'Crd acpt id': 'ISPA/PULSE',
'Crd acpt loc': '000',
'Crd acpt trmid': 'CS61252',
'Ddsi data len': '022',
'Ddsi data map': 'B2',
'Exp date': '24/02',
'Info text': 'NI24PS20ID16',
'Local date': '06/13',
'Local time': '02:46:37',
'Merchant': '6011',
'Msg-type': '0220',
'NAME Tran curr': '840',
'Natl cond code': '1010000002U',
'Pos entry': '051',
'Proc code': '312000',
'Processor id': 'INT789',
'Pseudo term': '070792',
'Reason codes': '004',
'Resp code': '00',
'Retr ref': '111111111111',
'Rsn code map': '40',
'Sett date': '06/13',
'TH AVENUE BOREALIS XXUS Name': 'MERCHANT',
'Trace nbr': '000000',
'Tran amt': '000000000000',
'Tran datetime': '0613064645',
'Xbit-map': '0000000000000010'}
Upvotes: 1