如何将以下python字典解析为键值对?
{'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'}
需要输出:
'rt_dur' : '9.490000000000009'
'rt_fet_st': '116.625'
感谢任何帮助?
答案 0 :(得分:2)
尝试正则表达式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'}