python-3.x - 如何删除字典的前几行?
问题描述
这是我的代码
# 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'}]}}
解决方案
似乎您只想要“项目”,因此您可以使用以下方式访问:output['list']['item']
。
推荐阅读
- c# - 如何在 UPDATE 命令的 WHERE 子句中使用三个条件更新单个记录?
- django - 如果我不复制 API 端点,Django Get 请求将返回空数组
- scala - scala 对象组合 - 访问不同类型的嵌套对象字段(例如 Array 与 Option[Int])
- javascript - 如何从 node.js 调用 JSON 中的数组字段
- visual-studio - 在另一个帐户下运行 Visual Studio 2017 时,Windows 身份验证不适用于 IIS Express 10
- activemq - ActiveMQ MessageGroupHashBucket - 需要什么缓存属性?
- python - Python set() 和 list() 函数一起使用
- java - 在 jni 中运行的原生 C 代码与在 Android 中的 shell 中运行的区别
- javascript - 混淆除白名单之外的所有查询参数
- python - 3.65上的python winapi安装