-1

How to parse the following python dictionary as key value pair?

{'restiming[0][rt_dur]': '9.490000000000009', 
 'restiming[1][rt_fet_st]': '116.625', 
 'restiming[0][rt_st]': '116.625', 
 'restiming[0][rt_name]': 'http://localhost:63342/Beacon/boomerang/boomerang.js',  
 'restiming[1][rt_in_type]': 'script'}

output needed:

'rt_dur' : '9.490000000000009'
'rt_fet_st': '116.625'

Any help is appreciated?

Psidom
  • 209,562
  • 33
  • 339
  • 356
Maddy_15
  • 61
  • 5
  • 2
    Unclear what you're asking. Is the input a string or is it an actual dictionary? If it is, it's already in key/value format, and you can choose to print whichever keys you want. – xgord Jul 08 '16 at 21:09
  • possibly duplicate of http://stackoverflow.com/questions/14323000/parsing-dictionary-like-url-parameters-in-python – Arnial Jul 08 '16 at 21:15

1 Answers1

2

Try regular expression re,

In [36]: import re
In [37]: {re.sub('restiming\[\d+\]\[(.*)\]', r'\1', k):v for k,v in data.iteritems()}
Out[37]:
{'rt_dur': '9.490000000000009',
 'rt_fet_st': '116.625',
 'rt_in_type': 'script',
 'rt_name': 'http://localhost:63342/Beacon/boomerang/boomerang.js',
 'rt_st': '116.625'}
r44
  • 434
  • 6
  • 5