gpt4 book ai didi

python - 如何正确设置此 API 调用的格式?

转载 作者:行者123 更新时间:2023-12-01 09:06:17 25 4
gpt4 key购买 nike

我正在制作一个电报聊天机器人,但不知道如何从输出中取出 [{'

def tether(bot, update):
tetherCall = "https://api.omniexplorer.info/v1/property/31"
tetherCallJson = requests.get(tetherCall).json()
tetherOut = tetherCallJson ['issuances'][:1]
update.message.reply_text("Last printed tether: " + str (tetherOut)+" Please take TXID and past it in this block explorer to see more info: https://www.omniexplorer.info/search")

我的用户将看到此响应:[{'grant': '25000000.00000000', 'txid': 'f307bdf50d90c92278265cd92819c787070d6652ae3c8af46fa6a96278589b03'}]

最佳答案

这看起来像一个包含单个字典的列表:

[{'grant': '25000000.00000000',
'txid': 'f307bdf50d90c92278265cd92819c787070d6652ae3c8af46fa6a96278589b03'}]

您应该能够通过使用 [0] 对列表进行索引来访问字典...

tetherOut[0]
# {'grant': '25000000.00000000',
# 'txid': 'f307bdf50d90c92278265cd92819c787070d6652ae3c8af46fa6a96278589b03'}

...如果您想从字典中获取特定值,您可以按其名称进行索引,例如

tetherOut[0]['txid']
# 'f307bdf50d90c92278265cd92819c787070d6652ae3c8af46fa6a96278589b03'

但是要小心链接这些东西。如果 tetherOut 是空列表,tetherOut[0] 将生成 IndexError。您可能想要捕获它(以及无效字典键将生成的 KeyError)。

关于python - 如何正确设置此 API 调用的格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52020056/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com