如何在Python中解析以下输入

2024-10-01 09:33:15 发布

您现在位置:Python中文网/ 问答频道 /正文

如何将下面的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'

有什么需要帮忙的吗?你知道吗


Tags: nameinlocalhosthttp字典typejs键值
1条回答
网友
1楼 · 发布于 2024-10-01 09:33:15

尝试正则表达式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'}

相关问题 更多 >