如何使用Python对JSON值执行操作?

时间:2018-03-08 12:33:02

标签: python json python-3.x

我有一个我正在解析的JSON文件。作为我在文件上执行的操作的一部分,我需要在键yardLine中的值为"ORE"的任何元素中从100中减去"homeAbbr"键。如果代码更新原始yardLine密钥的值或创建具有修改值的新yardLineReal密钥并不重要 - 我已经尝试了两种方式,但似乎逻辑上,新键更容易创建,以便无限循环。这里是JSON的两个元素的样本 - 每个元素都有相同的键:

{
        "gameId": "400935253",
        "year": 2017,
        "week": 1,
        "homeId": "2483",
        "homeTeam": "Oregon",
        "homeAbbr": "ORE",
        "awayId": "253",
        "awayTeam": "Southern Utah",
        "awayAbbr": "SUU",
        "driveIndex": "2",
        "playIndex": "1",
        "offenseId": "2483",
        "offenseTeam": "Oregon",
        "offenseAbbr": "ORE",
        "defenseId": "253",
        "defenseTeam": "Southern Utah",
        "defenseAbbr": "SUU",
        "homeScore": 7,
        "awayScore": 7,
        "isScore": false,
        "quarter": 1,
        "clock": "11:39",
        "type": "Rush",
        "down": 1,
        "distance": 10,
        "yardLine": 26,
        "yardsGained": 14,
        "endYardLine": 40,
        "description": "Royce Freeman run for 14 yds to the Oregn 40 for a 1ST down"
    },
    {
        "gameId": "400935253",
        "year": 2017,
        "week": 1,
        "homeId": "2483",
        "homeTeam": "Oregon",
        "homeAbbr": "ORE",
        "awayId": "253",
        "awayTeam": "Southern Utah",
        "awayAbbr": "SUU",
        "driveIndex": "2",
        "playIndex": "2",
        "offenseId": "2483",
        "offenseTeam": "Oregon",
        "offenseAbbr": "ORE",
        "defenseId": "253",
        "defenseTeam": "Southern Utah",
        "defenseAbbr": "SUU",
        "homeScore": 7,
        "awayScore": 7,
        "isScore": false,
        "quarter": 1,
        "clock": "11:39",
        "type": "Rush",
        "down": 1,
        "distance": 10,
        "yardLine": 40,
        "yardsGained": 3,
        "endYardLine": 43,
        "description": "Royce Freeman run for 3 yds to the Oregn 43"
    }

这是我提出的非功能性代码,尝试使用编辑后的值创建一个名为yardLineReal的新密钥(我的非功能代码直接在{{1}中编辑值只有第8行中键的名称不同:

yardLine

我不相信代码会调用与import json with open ('data.json', 'r') as data_file: data = json.load(data_file) for element in data: if 'ORE' in element["homeAbbr"]: data[yardLineReal] = 100 - data[yardLine] with open('data.json', 'w') as data_file: data = json.dump(data, data_file) 相关联的值。我想这个问题归结为,有没有办法引用这个值?

谢谢:)

3 个答案:

答案 0 :(得分:0)

当然,您可以将与“yardLine”相关联的值调用为

此代码正常工作!

for k, element in enumerate(data):
    if 'ORE' in element['homeAbbr']:
        data[k]['yardLine'] = 100 - element['yardLine']

结果:

{'awayAbbr': 'SUU',
  'awayId': '253',
  'awayScore': 7,
  'awayTeam': 'Southern Utah',
  'clock': '11:39',
  'defenseAbbr': 'SUU',
  'defenseId': '253',
  'defenseTeam': 'Southern Utah',
  'description': 'Royce Freeman run for 3 yds to the Oregn 43',
  'distance': 10,
  'down': 1,
  'driveIndex': '2',
  'endYardLine': 43,
  'gameId': '400935253',
  'homeAbbr': 'ORE',
  'homeId': '2483',
  'homeScore': 7,
  'homeTeam': 'Oregon',
  'isScore': False,
  'offenseAbbr': 'ORE',
  'offenseId': '2483',
  'offenseTeam': 'Oregon',
  'playIndex': '2',
  'quarter': 1,
  'type': 'Rush',
  'week': 1,
  'yardLine': 60, # before 40
  'yardsGained': 3,
  'year': 2017}

答案 1 :(得分:0)

您可以自行更新element。这将导致data也被更新:

import json

with open('data.json', 'r') as data_file:
    data = json.load(data_file)

for element in data:
    if 'ORE' in element['homeAbbr']:
        element['yardLineReal'] = 100 - element['yardLine']

with open('data.json', 'w') as data_file:
    data = json.dump(data, data_file, indent=4)

答案 2 :(得分:-2)

将一个JSON文件加载到Python后,它就变成了一个字典。您可以像这样迭代它:

for key, value in data.items():
    if value == something:
        new_value = do_something(value)
        data[key] = new_value

我不确定你到底想要做什么,但这应该可以帮助你开始。