如何正确格式化此 API 调用?
How do I properly format this API call?
我正在制作电报聊天机器人,但不知道如何从输出中取出 [{'
。
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
)。
我正在制作电报聊天机器人,但不知道如何从输出中取出 [{'
。
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
)。