我有以下输入数据的变体:
show_version_ios_parsed = {
"version": {
"uptime": "7 weeks, 6 days, 3 hours, 37 minutes",
}
}
和
show_version_nxos_parsed = {
"platform": {
"kernel_uptime": {"days": 102, "hours": 1, "minutes": 2, "seconds": 13},
}
}
我需要将uptime
(或kernel_uptime
)键值提取为字符串,以便两个结果看起来像这样:
{'uptime': '7 weeks, 6 days, 3 hours, 37 minutes'}
和
{'uptime': '102 days, 1 hours, 2 minutes, 13 seconds}
我提出了以下glom规范:
spec = (
T.items(),
Iter(
{
"uptime": Coalesce(T[1]["uptime"], T[1]["kernel_uptime"]),
},
),
Merge(),
)
将其应用于输入数据后,我得到以下信息:
>>> glom(show_version_ios_parsed, spec)
{'uptime': '7 weeks, 6 days, 3 hours, 37 minutes'}
>>> glom(show_version_nxos_parsed, spec)
{'uptime': {'days': 102, 'hours': 1, 'minutes': 2, 'seconds': 13}}
如您所见,它仅uptime
按原样提取键值。
有没有办法将第二个正常运行时间值转换为带有glom的字符串?