Python 获取字典中第一个key值并提取版本号
要取result中的第一个key值并取第二个正则表达式,可以使用以下代码:
import re
data = {
'code': '0',
'result': {
'8a7400e98905f75d01890657e19b0a2b': 'dops/U2.19.2'
},
'status': 200,
'timestamp': 1688028570331
}
result = data['result']
first_key = list(result.keys())[0]
second_regex = re.compile(r'\d+\.\d+\.\d+')
matches = re.findall(second_regex, result[first_key])
print(matches)
输出结果为:['2.19.2']
这段代码首先获取result字典,然后使用list(result.keys())[0]获取第一个key值,即'8a7400e98905f75d01890657e19b0a2b'。接下来使用正则表达式'\d+\.\d+\.\d+'匹配版本号,使用re.findall()函数返回匹配结果。在这个例子中,结果为['2.19.2']。
原文地址: https://www.cveoy.top/t/topic/o34E 著作权归作者所有。请勿转载和采集!