首页 > 解决方案 > 从json文件中提取信息,然后使用python排序

问题描述

我想从中提取“排名”信息的 Json 文件

基本上我想获得排名前 10 位的加密货币名称。每种加密货币在给定的 json 屏幕截图中都有其排名。无论如何我可以在python中实现这个吗?

提供指向所显示图像的链接 https://api.nomics.com/v1/currencies/ticker?key=demo-26240835858194712a4f8cc0dc635c7a

标签: pythonjsondiscord.py

解决方案


尝试这个:

import json
# your saved json response from API
file_path = "full/path/to/file.json"
with open(file_path, "r") as f:
    data = json.load(f)
    top_10_names = [x["name"] for x in data[:10]]
    # since the data is ordered by rank, 
    # you can take only the first 10 elements.
    print(top_10_names)


推荐阅读