英文:
Python last index (not per number)
问题
我尝试读取来自Kraken的TradesHistory,但使用索引无法正常工作。只有在输入正确的交易时才能正常工作。如何逐个访问每个交易?
使用以下命令,我可以获取所有交易。它有效。
print(resp.json()['result']['trades'])
如果我现在想要访问第一个索引,它不起作用。
print(resp.json()['result']['trades'][0]['ordertxid'])
只有在输入正确的交易ID后,我才能访问以下索引。
print(resp.json()['result']['trades']['12345-abcde-ZYXWE']['ordertxid'])
我做错了什么?如何在不事先知道ID的情况下访问正确的索引?
英文:
I'm trying to read the TradesHistory from krakenn but it doesn't work with the index. It only works if I enter the right trade. How can I step through each individual trade individually?
With the following command I get all the trades. It works.
print(resp.json()['result']['trades'])
If I wanted to access the first index now, it doesn't work.
print(resp.json()['result']['trades'][0]['ordertxid'])
Only after entering the correct trade id can I access the following index.
print(resp.json()['result']['trades']['12345-abcde-ZYXWE']['ordertxid'])
What am I doing wrong? How can I access the correct index without first knowing the ID?
{
   "error":[
      
   ],
   "result":{
      "count":2,
      "trades":{
         "12345-abcde-ZYXWE":{},
         "ZYXWE-12345-abcde":{
            "ordertxid":"xyz",
         }
      }
   }
}
various Python index commands with different JSON formats
答案1
得分: 0
为了浏览所有交易,您可以循环遍历已经完成的交易并提取它们的信息。
您可以像这样使用 for 循环:
trades = resp.json()['result']['trades']
for trade in trades: # 访问每笔交易
    if trades[trade] == {}:
        print('交易 {} 为空。'.format(trade))
        continue
    print('{} 的交易信息:'.format(trade))
    for value in trades[trade]: # 访问交易字典中的每个值
        print(value, ':', trades[trade][value])
这段代码用于遍历并提取交易信息。
英文:
In order to go over all the trades, you can loop over the trades that were made and extract their information.
You can use a for loop like this:
trades = resp.json()['result']['trades']
for trade in trades: # accessing each trades
    if trades[trade] == {}:
        print('trade {} is empty.'.format(trade))
        continue
    print('{}\'s trade information:'.format(trade))
    for value in trades[trade]: # accessing each value inside the trade dictionary
        print(value, ':', trades[trade][value])
答案2
得分: 0
I tried with this code below it is working.
import json
f = open('test.json')
test = json.load(f)
test['result']['trades']['ZYXWE-12345-abcde']['ordertxid']
Output
'xyz'
And looking at your code it looks like you are referring to the wrong index here ['12345-abcde-ZYXWE']
英文:
I tried with this code below it is working.
import json
f = open('test.json')
test  = json.load(f)
test['result']['trades']['ZYXWE-12345-abcde']['ordertxid']
Output
'xyz'
And looking at your code it looks like you are referring to wrong index here ['12345-abcde-ZYXWE']
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论