简体   繁体   English

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

[英]Retrieve specific data from JSON file in Python

Hello everyone and happy new year, I'm posting for the first time here since I can't find an answer for my problem.大家好,新年快乐,我第一次在这里发帖,因为我找不到我的问题的答案。 I've been searching for 2 days now and can't figure out what to do and it's driving me crazy..: Here's my problem:我现在已经搜索了 2 天,不知道该怎么做,这让我发疯了..:这是我的问题:

I'm using the Steamspy api to download data of the 100 most played games in the last 2 weeks on Steam, I'm able to dump the downloaded data into a.json file (dumped_data.json in this case), and to import it back into my Python code (I can print it).我正在使用Steamspy api 下载过去两周在 Steam 上玩得最多的 100 款游戏的数据,我可以将下载的数据转储到 a.json 文件(dumped_data.Z466DEEC76ECDF5FCA6D38 中的 4 到它回到我的 Python 代码(我可以打印它)。 And while I can print the whole thing, it's not really useable as it's 1900 lines of more or less useful data, so I want to be able to print only specific items of the dumped data.虽然我可以打印整个内容,但它并不是真正有用的,因为它有 1900 行或多或少有用的数据,所以我希望能够只打印转储数据的特定项目。 In this project, I would like to only print the name, developer, owners and price of the first 10 games on the list.在这个项目中,我只想打印列表中前 10 款游戏的名称、开发商、所有者和价格。 It's my first Python project, and I can't figure out how to do this...这是我的第一个 Python 项目,我不知道该怎么做......

Here the Python code:这里的 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)

And here's an exemple of the first item in the json file:这是 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
},

Thanks in advance to everyone that's willing to help me, it would mean a lot.提前感谢所有愿意帮助我的人,这意味着很多。

The following prints the values you desire from the first 10 games:以下打印了前 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