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
361 views
in Technique[技术] by (71.8m points)

python 3.x - Is there an easy way to use glom to get an unknown key from a dictionary?

Is there an easy way to use glom to get an unknown key from a dictionary?

the ??? represents random data that I am trying to capture from an API

    book = {"???":[{ "globalIdenity":208565940},{"globalIdenity":228049454}]}

    spec = 
    output_data = glom(book, spec)
    print(output_data)

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

1 Answer

0 votes
by (71.8m points)

Use an iterator for values then use the next call to get the value

book = {"???":[{ "globalIdenity":208565940},{"globalIdenity":228049454}]}

result = next(iter(book.values()))

print(result)

#output
[{'globalIdenity': 208565940}, {'globalIdenity': 228049454}]

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

...