Python 如何循环使用UID';他在火力基地
我使用Python遍历Firebase DB以返回一个对象数组,然后随机选取一个对象并返回其值。我一直在使用一个小型的测试JSON数据库,我手动构建并导入Firebase。当我这样做时,DB的子节点是Python 如何循环使用UID';他在火力基地,python,arrays,json,firebase,firebase-realtime-database,Python,Arrays,Json,Firebase,Firebase Realtime Database,我使用Python遍历Firebase DB以返回一个对象数组,然后随机选取一个对象并返回其值。我一直在使用一个小型的测试JSON数据库,我手动构建并导入Firebase。当我这样做时,DB的子节点是0、1、2等。。。使用下面的代码-我可以遍历数据并获取所需的内容 我一直在构建一个CMS,它允许我使用push()方法将数据直接输入Firebase(而不是导入本地JSON文档) 相应地,子节点变得像这样的模糊时间戳:K036VOR90fh8sd80、KO698fhs7Hf8sfds等 现在,当我尝
0、1、2
等。。。使用下面的代码-我可以遍历数据并获取所需的内容
我一直在构建一个CMS,它允许我使用push()
方法将数据直接输入Firebase(而不是导入本地JSON文档)
相应地,子节点变得像这样的模糊时间戳:K036VOR90fh8sd80、KO698fhs7Hf8sfds
等
现在,当我尝试通过节点进行for循环时,在ln9
caption=..
处出现以下错误:
TypeError:字符串索引必须是整数
我假设这是因为子节点现在是字符串。既然我必须使用CMS,我现在如何循环通过这些节点
代码:
以下是Firebase的大致外观:
{
"1001" : {
"-K036VOR90fh8sd80EQ" : {
"audioPath" : "https://s3.amazonaws.com/bucket-output/audio/audio_0_1001.mp3",
"caption" : "Record 0 from 1001"
},
"-KO698fhs7Hf8sfdsWJS" : {
"audioPath" : "https://s3.amazonaws.com/bucket-output/audio/audio_1_1001.mp3",
"caption" : "Record 1 from 1001"
}
},
"2001" : {
"-KOFsPBMKVtwHSOfiDJ" : {
"audioPath" : "https://s3.amazonaws.com/bucket-output/audio/audio_0_2001.mp3",
"caption" : "Record 0 from 2001"
},
"-KOFsQvwgF9icSIolU" : {
"audioPath" : "https://s3.amazonaws.com/bucket-output/audio/audio_1_2001.mp3",
"caption" : "Record 1 from 2001"
}
}
}
需要做的是通过键值(
k,v
)和附加值(v
),使用for循环
和.items()
Python方法剥离父节点的字典结果
这将剥离父Firebase字典键的结果
,即-KOFsQvwgF9icSIolU
if 'Item' in intent['slots']:
chosen_item = intent['slots']['Item']['value']
result = firebase.get(chosen_item, None)
if result:
item_object = []
for k,v in result.items():
item_object.append(v)
random_item = (random.choice(item_object))
caption = random_item['caption']
audio_src = random_item['audioPath']
你能添加一个链接到你用来访问Firebase的Python库吗?@FrankvanPuffelen是的
if 'Item' in intent['slots']:
chosen_item = intent['slots']['Item']['value']
result = firebase.get(chosen_item, None)
if result:
item_object = []
for k,v in result.items():
item_object.append(v)
random_item = (random.choice(item_object))
caption = random_item['caption']
audio_src = random_item['audioPath']