繁体   English   中英

从 Python 中的 JSON 文件中检索特定数据

[英]Retrieve specific data from JSON file in Python

大家好,新年快乐,我第一次在这里发帖,因为我找不到我的问题的答案。 我现在已经搜索了 2 天,不知道该怎么做,这让我发疯了..:这是我的问题:

我正在使用Steamspy api 下载过去两周在 Steam 上玩得最多的 100 款游戏的数据,我可以将下载的数据转储到 a.json 文件(dumped_data.Z466DEEC76ECDF5FCA6D38 中的 4 到它回到我的 Python 代码(我可以打印它)。 虽然我可以打印整个内容,但它并不是真正有用的,因为它有 1900 行或多或少有用的数据,所以我希望能够只打印转储数据的特定项目。 在这个项目中,我只想打印列表中前 10 款游戏的名称、开发商、所有者和价格。 这是我的第一个 Python 项目,我不知道该怎么做......

这里的 Python 代码:

import steamspypi
import json

#gets data using the steamspy api
data_request = dict()
data_request["request"] = "top100in2weeks"

#dumps the data into json file
steam_data = steamspypi.download(data_request)
with open("dumped_data.json", "w") as jsonfile:
    json.dump(steam_data, jsonfile, indent=4)

#print values from dumped file
f = open("dumped_data.json")
data= json.load(f)

print(data)

这是 json 文件中第一项的示例:

{
"570": {
    "appid": 570,
    "name": "Dota 2",
    "developer": "Valve",
    "publisher": "Valve",
    "score_rank": "",
    "positive": 1378093,
    "negative": 267290,
    "userscore": 0,
    "owners": "100,000,000 .. 200,000,000",
    "average_forever": 35763,
    "average_2weeks": 1831,
    "median_forever": 1079,
    "median_2weeks": 1020,
    "price": "0",
    "initialprice": "0",
    "discount": "0",
    "ccu": 553462
},

提前感谢所有愿意帮助我的人,这意味着很多。

以下打印了前 10 场比赛中您想要的值:

for game in list(data.values())[:10]:
    print(game["name"], game["developer"], game["owners"], game["price"])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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