首页 > 解决方案 > 如何删除字典的前几行?

问题描述

这是我的代码

# Our search function
def ndb_search(q):
    """Returns terms regarding food's foodgroup, name, NBD number, data source, and manufacturer from the USDA Food Composition Databases based on your 'q'"""
    response = requests.get("https://api.nal.usda.gov/ndb/search", params = {
        'api_key': key,
        'q': q,
        'offset': 0,
        'fg': "",
        'name': "",
        'ndbno': "",
        'ds': "",
        'manu': ""
    })
    # Checks if we get a HTTP status code back
    response.raise_for_status()
    # Converts out JSON format information into lists and dictionaries
    search_output = response.json()
    return search_output

ndb_search("quail eggs")

这是我得到的输出: 我得到的输出

这是我想要的输出: 我想要的输出

如果有帮助,这是我要操作的字典:

search_ output = {'list': {'q': 'quail eggs', 'sr': '1', 'ds': 'any', 'start': 0, 'end': 6, 'total': 6, 'group': '', 'sort': 'r', 'item': [{'offset': 0, 'group': 'Branded Food Products Database', 'name': 'EL COMPI, QUAIL EGGS, UPC: 854955002226', 'ndbno': '45362205', 'ds': 'LI', 'manu': "milly's desserts llc"}, {'offset': 1, 'group': 'Branded Food Products Database', 'name': 'BUDDHA, QUAIL EGGS IN BRINE, UPC: 761934535098', 'ndbno': '45099560', 'ds': 'LI', 'manu': 'Sung Ly International Corporation'}, {'offset': 2, 'group': 'Branded Food Products Database', 'name': 'GRAN SABANA, QUAIL EGGS, UPC: 819140010103', 'ndbno': '45169279', 'ds': 'LI', 'manu': 'L & M C Farms, Inc.'}, {'offset': 3, 'group': 'Branded Food Products Database', 'name': 'L&W, QUAIL EGGS, UPC: 024072000256', 'ndbno': '45094890', 'ds': 'LI', 'manu': 'L&W International Co.'}, {'offset': 4, 'group': 'Branded Food Products Database', 'name': 'CHAOKOH, QUAIL EGG IN BRINE, UPC: 044738074186', 'ndbno': '45094707', 'ds': 'LI', 'manu': 'Theppadung Porn Coconut Co'}, {'offset': 5, 'group': 'Dairy and Egg Products', 'name': 'Egg, quail, whole, fresh, raw', 'ndbno': '01140', 'ds': 'SR', 'manu': 'none'}]}}

标签: python-3.x

解决方案


似乎您只想要“项目”,因此您可以使用以下方式访问:output['list']['item']


推荐阅读