拉"玩家" "统计"来自JSON

时间:2017-05-22 16:12:37

标签: python json list io

我有一个非常大的JSON文件,有数千个模拟篮球游戏,每个游戏中的每个玩家都有统计数据。我希望最终可以将它们推送到csv,但是现在我想做的就是能够将数据作为字符串输入到数组中。

我编写的代码,但似乎没有做我想要它做的事情,stats = []我定义写入统计数据仍然是空的。

到目前为止,这是我的计划:

import os

#create path for file
path = os.path.dirname('C:\\tmp\\Season3Playoffs.json')
#create list for stats for each player
stat = []
#open file
for filename in os.listdir(path):
    with open(os.path.join(path, filename), encoding = "UTF-8") as input_file:
        #go through each line of the file
        for line in input_file:
            #if a line has "pid" copy contents to the list stat until "psid" is encountered
            if line in ('"pid"', '"psid"'):
                stat.append(line.rstrip('\n'))
                #print the stat entry
                print(stat)
print("program complete")

以下是我希望在列表的每个元素中保存的JSON文件的一部分的示例:

"value": 64.8,
      "valueNoPot": 72,
      "valueFuzz": 64.8,
      "valueNoPotFuzz": 72,
      "valueWithContract": 64.8,
      "pid": 1,
      "imgURL": "",
      "firstName": "Chris",
      "lastName": "Bosh",
      "diedYear": null,
      "stats": [
        {
          "pid": 1,
          "season": 2015,
          "tid": 27,
          "playoffs": false,
          "gp": 0,
          "gs": 0,
          "min": 0,
          "fg": 0,
          "fga": 0,
          "fgAtRim": 0,
          "fgaAtRim": 0,
          "fgLowPost": 0,
          "fgaLowPost": 0,
          "fgMidRange": 0,
          "fgaMidRange": 0,
          "tp": 0,
          "tpa": 0,
          "ft": 0,
          "fta": 0,
          "pm": 0,
          "orb": 0,
          "drb": 0,
          "trb": 0,
          "ast": 0,
          "tov": 0,
          "stl": 0,
          "blk": 0,
          "ba": 0,
          "pf": 0,
          "pts": 0,
          "per": 0,
          "ewa": 0,
          "yearsWithTeam": 1,
          "psid": 0
        }
      ]

从那段代码我希望文本介于" pid:x"和" psid:"保存在stat [x]中。

我不明白为什么它没有写入数组,非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

以下可能有用。

import json
data = json.loads("""{
    "value": 64.8,
    "valueNoPot": 72,
    "valueFuzz": 64.8,
    "valueNoPotFuzz": 72,
    "valueWithContract": 64.8,
    "pid": 1,
    "imgURL": "",
    "firstName": "Chris",
    "lastName": "Bosh",
    "diedYear": null,
    "stats": [
        {
            "pid": 1,
            "season": 2015,
            "tid": 27,
            "playoffs": false,
            "gp": 0,
            "gs": 0,
            "min": 0,
            "fg": 0,
            "fga": 0,
            "fgAtRim": 0,
            "fgaAtRim": 0,
            "fgLowPost": 0,
            "fgaLowPost": 0,
            "fgMidRange": 0,
            "fgaMidRange": 0,
            "tp": 0,
            "tpa": 0,
            "ft": 0,
            "fta": 0,
            "pm": 0,
            "orb": 0,
            "drb": 0,
            "trb": 0,
            "ast": 0,
            "tov": 0,
            "stl": 0,
            "blk": 0,
            "ba": 0,
            "pf": 0,
            "pts": 0,
            "per": 0,
            "ewa": 0,
            "yearsWithTeam": 1,
            "psid": 0
        }
    ]
}""")

print(data['stats'])