When starting, it prints data in the form of a dictionary, but when you try to print print (data [0]), an error of key_error 0 pops up. Lost in three pines, help me figure it out.

def request_book(api_method, params_dict): params_dict['nonce'] = int(round(time.time() * 1000)) params_urlencoded = urllib.parse.urlencode(params_dict) sign_bytes = get_signature(params_urlencoded) headers = { 'Content-Type': 'application/x-www-form-urlencoded', 'Key': PUBLIC_KEY, 'Sign': base64.b64encode(sign_bytes).decode('utf-8') } response = requests.get( urllib.parse.urljoin(TRANSACTION_API_URL, api_method), headers=headers, data=params_urlencoded) try: response_json = response.json() if response_json['success'] == 0: print('Received error: {}'.format(response_json['error'])) return response_json except Exception as ex: #print(response.json()) return response_json if __name__ == '__main__': data =request_book('/mapi/v2/depth/eurusd', {}) print(data) 

this is a dictionary that prints print (data)

 {'eurusd': {'asks': [[1.147, 2000], [1.148271, 29190.8], [1.148278, 77046.3], [1.148285, 76735.2], [1.148292, 3555.1], [1.148293, 5870.9], [1.148297, 64921.8], [1.148299, 28708.8], [1.148302, 98302.3], [1.148306, 95492.2], [1.148319, 22526.6], [1.14832, 44616.7], [1.148324, 19006.9], [1.148328, 12004.8], [1.148331, 39137.3], [1.148357, 67219.1], [1.148366, 39256], [1.14837, 21136.3], [1.148371, 73958.5], [1.148376, 59804.2], [1.148378, 77444.5], [1.14838, 98295.3], [1.148384, 61911.1], [1.148385, 56456.3], [1.148389, 525000], [1.148391, 71467.2], [1.148393, 19524.44], [1.148396, 53030.1], [1.148399, 3100000], [1.148418, 82985.2], [1.148423, 71374.6], [1.148426, 53423.2], [1.148431, 75243.6], [1.148441, 70035.3], [1.148447, 6333.9], [1.148451, 10570.1], [1.148467, 37142.2], [1.148468, 73362.3], [1.148479, 72116.4], [1.1485, 23859.2], [1.148503, 56489.9], [1.148505, 197141], [1.148506, 80696.4], [1.148509, 60660.4], [1.14851, 53577.8], [1.148513, 97791.6], [1.14853, 95873.1], [1.148536, 834758.7], [1.148538, 95015.3], [1.148539, 500000], [1.148542, 579760.5], [1.148549, 51364.4], [1.148552, 84633.3], [1.148553, 93335.2], [1.148592, 77570.1], [1.148603, 25010.4], [1.148611, 41763.8], [1.14863, 2571314.8], [1.148635, 20980.5], [1.148654, 72429.9], [1.148655, 19906.7], [1.148677, 95119.1], [1.14868, 31406.6], [1.148689, 38952.2], [1.148695, 775000], [1.148696, 95030.3], [1.148699, 96462.5], [1.148701, 43143.4], [1.148703, 82339.3], [1.148707, 34728.6], [1.148711, 1248447.6], [1.148713, 82156.4], [1.148719, 58296.6], [1.148724, 71756.1], [1.148747, 89336.6], [1.148758, 500000], [1.148759, 1000000], [1.148761, 15171.9], [1.148763, 40327.1], [1.14877, 700000], [1.148774, 49068.1], [1.148779, 53401.1], [1.148783, 56378.6], [1.148784, 500000], [1.148788, 3000000], [1.148789, 500000], [1.14879, 73391.2], [1.148814, 80166.1], [1.148828, 84488.9], [1.148834, 89687.8], [1.148836, 64280.1], [1.148846, 28877.3], [1.14885, 19243.4], [1.148928, 1100000], [1.148966, 2100000], [1.1503, 1281.7], [1.154, 1], [1.15499, 190.43], [1.155, 641], [1.1555, 500], [1.155999, 500], [1.156, 500], [1.1565, 500], [1.157, 500], [1.158, 500], [1.1585, 58.91], [1.1587, 500], [1.159, 500], [1.16, 2000], [1.163, 500], [1.179, 501], [1.199, 24.3], [1.2486, 20], [1.295, 8.9]], 'bids': [[1.139, 1040.2], [1.138999, 17], [1.136999, 29], [1.135999, 29], [1.135567, 58], [1.135302, 34], [1.135, 103], [1.134999, 29], [1.133999, 105], [1.133852, 202.5], [1.13346, 67], [1.132999, 29], [1.132002, 1840], [1.132, 1816.8], [1.131002, 2042.8], [1.131, 5500], [1.130999, 29], [1.13046, 56], [1.130099, 55], [1.130067, 171], [1.129999, 70], [1.129499, 150], [1.129388, 41], [1.129199, 41], [1.128999, 29], [1.128599, 167], [1.128499, 29], [1.127999, 29], [1.127499, 29], [1.126999, 29], [1.126499, 29], [1.126, 500], [1.125999, 29], [1.125499, 160], [1.125477, 14232.9], [1.125468, 72377.1], [1.125453, 4864.5], [1.125449, 58544.5], [1.125446, 59514.2], [1.125442, 106831.3], [1.125414, 1018073.3], [1.125403, 18916.7], [1.125391, 68484.8], [1.125377, 78192.6], [1.125372, 85260.7], [1.125366, 40937], [1.12535, 32716.2], [1.125345, 63608.7], [1.125332, 1500000], [1.125328, 105679.6], [1.125323, 1600000], [1.125319, 97836.8], [1.125312, 9999.1], [1.125311, 46583.7], [1.12531, 27764.5], [1.125308, 109703.9], [1.125304, 71011], [1.125287, 33041.3], [1.125286, 30204], [1.125285, 36923.6], [1.125283, 39013.9], [1.125257, 94277.8], [1.125252, 46075.4], [1.125248, 28484.1], [1.125246, 130701.4], [1.125243, 62362.5], [1.125241, 22003.2], [1.12524, 11459.1], [1.125234, 31652.7], [1.125228, 75576.9], [1.125218, 65327.8], [1.125215, 62319], [1.125213, 500000], [1.125198, 54522.3], [1.125197, 35725.8], [1.125189, 31933.9], [1.125184, 14032.8], [1.12518, 80293], [1.125179, 91764.9], [1.125178, 72922], [1.125165, 71045], [1.12516, 56718], [1.125153, 15103.8], [1.12515, 62865.4], [1.125149, 1000000], [1.125143, 76709.3], [1.125139, 15873.3], [1.125136, 7555.5], [1.125132, 83348.3], [1.125131, 20278.2], [1.125125, 1025000], [1.125123, 47763.8], [1.125119, 400000], [1.125108, 52263.4], [1.125104, 8184.7], [1.125101, 98650], [1.1251, 88001.6], [1.125094, 123366.8], [1.125089, 78371.4], [1.125081, 62453.7], [1.125079, 41388.8], [1.12507, 843986], [1.125067, 39877.4], [1.125064, 48664.3], [1.125059, 3000000], [1.125055, 40666.4], [1.12505, 53916.2], [1.125041, 94640.6], [1.125023, 84067.4], [1.125021, 96556.7], [1.12502, 18951.2], [1.125013, 500000], [1.125, 528], [1.124999, 29], [1.124994, 2100000], [1.124987, 1025000], [1.124985, 1275000], [1.124983, 47775.8], [1.12498, 88369.6], [1.124948, 4385.7], [1.124941, 1441.7], [1.12492, 14880.7], [1.12491, 500000], [1.124907, 2100000], [1.124903, 89363.7], [1.124902, 80765.1], [1.124897, 2000000], [1.124893, 500000], [1.123999, 29], [1.12302, 95], [1.122499, 131], [1.121, 400], [1.120875, 23], [1.12014, 330], [1.12, 187], [1.11995, 34], [1.11602, 95.5], [1.116, 300], [1.115, 1075], [1.113852, 5000], [1.11144, 142.8], [1.11111, 232], [1.111, 5300], [1.11, 2818], [1.106, 300], [1.105, 96.3], [1.103, 79.1], [1.101, 333], [1.091276, 57], [1.091202, 70], [1.081276, 114], [1.071276, 114], [1.071, 37], [1.06767, 99], [1.064567, 121], [1.061276, 57], [1.05599, 99], [1.051276, 114], [1.04899, 99], [1.041276, 114], [1.03499, 99], [1.031276, 57], [1.02499, 99], [1.021276, 57], [1.01499, 99], [1.01, 414], [1.0, 144], [0.993367, 45], [0.983367, 45], [0.973367, 45], [0.963367, 123], [0.953367, 78], [0.943367, 78], [0.938367, 78], [0.937367, 78], [0.935367, 78], [0.933367, 78], [0.925367, 78], [0.923367, 78], [0.919367, 78], [0.913367, 78], [0.909367, 78], [0.906367, 78], [0.904367, 78], [0.903367, 78], [0.902367, 78]], 'market_buy_price': 1.295, 'market_sell_price': 0.902367}} 
  • can you publish the get_signature function? - S. Nick
  • I am trying to give the code in a normal way, but for some reason during the posting there are no indents DSX_URL = " dsx.uk " TRANSACTION_API_URL = urllib.parse.urljoin (DSX_URL, ") def to_bytes (str): return str.encode ('utf- 8 ') def get_signature (params_urlencoded): hmac_obj = hmac.new (to_bytes (SECRET_KEY), to_bytes (params_urlencoded), hashlib.sha512) return hmac_obj.digest () - Vitaly Tolokonnikov
  • All code should be in the question text, comments are not intended for code - andreymal
  • You write that print(data[0]) выскакиваСт ошибка key_error 0. , and what print(data[0]) выскакиваСт ошибка key_error 0. print(data) ? - S. Nick
  • print (data) prints the dictionary. At the same time, if print (data ['eurusd']) is typed, then no error is produced. I can and so work, but already a matter of principle. Maybe I'm just wrong to appeal to the key? - Vitaly Tolokonnikov

1 answer 1

A dictionary (or associative array) is a data structure that allows to identify its elements not by a numerical index , but by an arbitrary one .

An index can be any immutable value, for example:

 data = { "some_key": 1, # ΠΊΠ»ΡŽΡ‡ строка, ΠΎΠΊ! 1: ["1", "2"], # ΠΊΠ»ΡŽΡ‡ число, ΠΎΠΊ! (1,2,3): 1.0, # ΠΊΠ»ΡŽΡ‡ ΠΊΠΎΡ€Ρ‚Π΅ΠΆ, ΠΎΠΊ! Π΄Π°ΠΆΠ΅ ΠΊΠΎΡ€Ρ‚Π΅ΠΆ ΠΌΠΎΠΆΠ΅Ρ‚ Π±Ρ‹Ρ‚ΡŒ ΠΊΠ»ΡŽΡ‡ΠΎΠΌ, вСдь ΠΎΠ½ нСизмСняСмый } 

However, if the list when adding an element itself "creates" an index for it, then the dictionary does not add its indexes (well ... it adds it at all, but this concerns its internal structure). It creates a match between the specified key and the value (to quickly find this value).

Thus, when accessing data[0] in your case, the dictionary does not find this match (for key 0 it simply does not exist), so it swears and throws a KeyError .