简体   繁体   中英

handling json response data inside a list inside a dictionary python 3.6

i try to handle the odds from this betting site http://praktoreio.pamestoixima.gr/en/retail-football . With lots of help i pushed forward till the point i get some data

import requests
url = 'http://praktoreio.pamestoixima.gr/web/services/rs/
iFlexBetting/retail/games/15104/0.json?
shortTourn=true&startDrawNumber=686&endDrawNumber=686&sportId=s-
441&marketIds=0&marketIds=0A&marketIds=1&marketIds=69&marketIds=68&marketIds
=20&marketIds=21&marketIds=8&locale=en&brandId=defaultBrand&channelId=0'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) 
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36'}
resp = requests.get(url, headers=headers)
html = resp.json()
data_1 = html[0]['markets'][0]['codes'][0]['oddPerSet']['1']
data_X = html[0]['markets'][0]['codes'][1]['oddPerSet']['1']
data_2 = html[0]['markets'][0]['codes'][2]['oddPerSet']['1']
print('odds for win:', data_1)
print('odds for draw:', data_X)
print('odds for lose:', data_2)
data_CODE_0 = html[0]['code']
print('Game code:', data_CODE_0)

the thing is i get only those of element [0]. Instead of doing the above for every element is there a way to read float data inside a list inside a dictionary inside a list...automatically? Thanks in advance

I'm not entirely sure what you're asking, but you can store a reference and then loop through it. I'm also assuming when you say element[0] , you mean you want this behavior:

for element in html: # loops through list
    data = element['markets'][0]['codes']
        for x in data:
            print(x['oddPerSet']['1'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM