Decoding nested JSON with multiple 'for' loops

后端 未结 3 1149

I\'m new to Python (last week), and have reached my limit. Spent three days on this, most of my time in stackoverflow, but I cannot work out how to go any further!

T

相关标签:
3条回答
  • 2021-01-16 15:47
    import os, json,requests
    print 'Starting'
    url = 'https://dl.dropboxusercontent.com/u/3758695/json.txt'
    
    # download the json string
    json_string = requests.get(url)
    print 'Downloaded json'
    
    # get the content
    the_data = json_string.json()
    print 'the_data has length ', len(the_data)
    for index in range(len(the_data)):
        print 'Now working on index ', index
        for d in the_data[index]['innings']:
            print d['wickets']
    
    0 讨论(0)
  • 2021-01-16 15:49

    First of all, don't use an index but loop directly over the lists; that way you can give them meaningful names. The top-level is a list of entries, each entry is a dictionary with a 'innings' key, and each innings is a list of dictionaries, with, among others, a wickets key:

    for entry in data:
        for inning in entry['innings']:
            print inning['wickets']
    

    This prints:

    >>> for entry in data:
    ...     for inning in entry['innings']:
    ...         print inning['wickets']
    ... 
    10
    9
    0
    0
    

    This makes it easier to add information at each level too:

    >>> for entry in data:
    ...     print entry['description']
    ...     for i, inning in enumerate(entry['innings']):
    ...         print 'Innings {}: {} wickets'.format(i + 1, inning['wickets'])
    ... 
    Rest of Sri Lanka v Sri Lanka A at Pallekele, May 14, 2013
    Innings 1: 10 wickets
    Innings 2: 9 wickets
    63rd match: Royal Challengers Bangalore v Kings XI Punjab at Bangalore, May 14, 2013
    Innings 1: 0 wickets
    Innings 2: 0 wickets
    64th match: Chennai Super Kings v Delhi Daredevils at Chennai, May 14, 2013
    
    0 讨论(0)
  • 2021-01-16 16:10

    It looks ugly, but you can refine this, but here is listing at arbitrary depth of mix of Dict and List:

    import os, json,requests
    print 'Starting'
    url = 'https://dl.dropboxusercontent.com/u/3758695/json.txt'
    
    # download the json string
    json_string = requests.get(url)
    print 'Downloaded json'
    
    def dig_down(partial_json_list, depth):
        if type(partial_json_list) is list:
            for i in range(len(partial_json_list)):
                print 'index', i, ' at depth', depth,' has', len(partial_json_list[i]) , 'elements'
                if len(partial_json_list[i]) > 1:
                    dig_down(partial_json_list[i],depth+1)
        else:
            for k in partial_json_list:
                print 'item at depth', depth, 'equals', k#, ' & has', len(partial_json_list[k]) , 'elements'
                if type(partial_json_list) is list or type(partial_json_list) is dict:
                    try:
                        if len(partial_json_list[k]) > 1:
                            dig_down(partial_json_list[k],depth+1)
                    except:
                        pass
                else:
                    print partial_json_list[k]
    
    # get the content
    the_data = json_string.json()
    print 'the_data has length ', len(the_data)
    dig_down(the_data,0)
    
    0 讨论(0)
提交回复
热议问题