Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
158 views
in Technique[技术] by (71.8m points)

python - search for a dictionary key value using wildcard

There is any way to use a wildcard in order to skip a dictionary key. I have the following nested dictionary:

{'1': {'date': '25-Jan-21 12:45:15:788 ', 'payload':' ****AAAA** **
****'}, 
'2': {'date': '25-Jan-21 12:45:15:881 ', 'payload': ****'BBB**

**'}, 
'3': {'date': '25-Jan-21 12:45:15:897 ', 'payload': ' ****CCC**
**'}, 
'4': {'date': '25-Jan-21 12:45:15:907 ', 'payload': ' ****DDD** 
**'}}

And i want to ignore the first keys 1, 2 .... and extract only the value corresponding to payload key.

is possible to use some kind of wild card like i tried below?

for i in data['*']['payload']:
    print(i)
question from:https://stackoverflow.com/questions/65891364/search-for-a-dictionary-key-value-using-wildcard

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

simply iterate over it

for k1 in d:
    for k2 in d[k1]:
        payload = d[k1][k2]
        print(k2,'>>', payload)

output

date >> 25-Jan-21 12:45:15:788 
payload >> ****AAAA** **
****
date >> 25-Jan-21 12:45:15:881
payload >> ****BBB**

**
date >> 25-Jan-21 12:45:15:897
payload >> ****CCC**
**
date >> 25-Jan-21 12:45:15:907
payload >> ****DDD** 
**

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...