Extracting Data from OrderedDict

旧时模样 提交于 2021-01-28 03:46:50

问题


So i have a firebase database

and this is my code to get specific data

db = firebase.database()
test = db.child("Users").order_by_child("IDNumber").equal_to(222333123).get().val()

print(test)

then the result returns as an OrderedDict

OrderedDict([('Ays', {'Ays': 'Baby', 'IDNumber': 222333123})])

i want to extract the data and have Ays = Baby and IDNumber = 222333123 as two separate variables. i tried using .items() and putting it into list but i can't seem to separate it. is there any other way?


回答1:


There can be several items in the OrderedDict. It is always safe to iterate the list

from collections import OrderedDict
od = OrderedDict([('Ays', {'Ays': 'Baby', 'IDNumber': 222333123}), ('Ays1', {
    'Ays1': 'Baby1', 'IDNumber1': 222333123})])

for val in od.values():
    for k, v in val.items():
        print(k, v)

Output:

Ays Baby
IDNumber 222333123
Ays1 Baby1
IDNumber1 222333123



回答2:


You can use .values() to extract key, value

>>> d = OrderedDict([('Ays', {'Ays': 'Baby', 'IDNumber': 222333123})])
>>> list(d.values())
[{'Ays': 'Baby', 'IDNumber': 222333123}]


来源:https://stackoverflow.com/questions/54529103/extracting-data-from-ordereddict

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!