【问题标题】:How to create a JSON file from another JSON file based on IF condition! Python3如何根据 IF 条件从另一个 JSON 文件创建 JSON 文件! Python3
【发布时间】:2021-07-24 04:57:16
【问题描述】:

我有一个这样的 json 文件:

[
      {
            "id": "john",
            "title": "tech",
            "date": "2020/1/1",
            "shift": "DAYS"
      },
      {
            "id": "tom",
            "title": "tech",
            "date": "2021/5/5",
            "shift": "NIGHT"
      }
]

我需要根据此信息重新创建另一个 JSON 文件。例如:

如果班次 == 晚上:

shiftHourStart = 22:00:00

shiftHourEnd = 06:00:00

所以输出应该是这样的:

[
      {
            "id": "john",
            "title": "tech",
            "shiftHourStart" = "22:00:00",
            "shiftHourEnd" = "06:00:00"
            "shift": "NIGHT"
      },
      {
            "id": "tom",
            "title": "tech",
            "date": "2021/5/5",
            "shiftHourStart" = "06:00:00",
            "shiftHourEnd" = "15:00:00",
            "shift": "DAY"          
      }
]

【问题讨论】:

  • 到目前为止你尝试过什么?向我们展示您的代码!
  • 预期输出与您的描述不符。
  • 读取、处理、写入——这些是步骤,您可以单独解决。

标签: python json python-3.x


【解决方案1】:

你可以使用dict.update

data = [
      {
            "id": "john",
            "title": "tech",
            "date": "2020/1/1",
            "shift": "DAYS"
      },
      {
            "id": "tom",
            "title": "tech",
            "date": "2021/5/5",
            "shift": "NIGHT"
      }
]

shift_mapping = {
    'DAYS': { # or DAY whatever you have
        "shiftHourStart": "22:00:00",
            "shiftHourEnd": "06:00:00"
    },
    'NIGHT':{
        "shiftHourStart": "06:00:00",
        "shiftHourEnd": "15:00:00",
    }
}

for each_shift in data:
    if 'shift' in each_shift:
        each_shift.update(shift_mapping[each_shift['shift']])
    else:
        print('Warning!! shift key does not exists!')
    
print(data)
[{'id': 'john',
  'title': 'tech',
  'date': '2020/1/1',
  'shift': 'DAYS',
  'shiftHourStart': '22:00:00',
  'shiftHourEnd': '06:00:00'},
 {'id': 'tom',
  'title': 'tech',
  'date': '2021/5/5',
  'shift': 'NIGHT',
  'shiftHourStart': '06:00:00',
  'shiftHourEnd': '15:00:00'}]

【讨论】:

  • % python3 1.py Traceback(最近一次调用最后):文件“1.p​​y”,第 47 行,在 make_json(inputFile, jsonFilePath) 文件“1.p​​y”,第 34 行, 在 make_json each_shift.update(shift_mapping[each_shift['shift']]) KeyError: 'shift'
  • 这意味着不是所有的人都有密钥shift,在这种情况下你想做什么?放下那个?你能提供更大的输入吗?
【解决方案2】:

使用这个:

import json
json_text = """[
      {
            "id": "john",
            "title": "tech",
            "date": "2020/1/1",
            "shift": "DAYS"
      },
      {
            "id": "tom",
            "title": "tech",
            "date": "2021/5/5",
            "shift": "NIGHT"
      }
]
"""
n_dict = json.loads(json_text)
if n_dict[0]['shift'] == 'DAY':
    n_dict[0]["shiftHourStart"]="06:00:00"
    n_dict[0]["shiftHourEnd"] = "15:00:00"
else:
    n_dict[0]["shiftHourStart"]="22:00:00"
    n_dict[0]["shiftHourEnd"] = "06:00:00"
new_json = json.dumps(n_dict)
print(new_json)

根据您的情况进行编辑: 如果json文件是".\\file.json"那么,

import json
with open('.\\file.json') as f:
  json_text = json.load(f)
n_dict = json.loads(json_text)
if n_dict[0]['shift'] == 'DAY':
    n_dict[0]["shiftHourStart"]="06:00:00"
    n_dict[0]["shiftHourEnd"] = "15:00:00"
else:
    n_dict[0]["shiftHourStart"]="22:00:00"
    n_dict[0]["shiftHourEnd"] = "06:00:00"
with open('.\\new_file.json', 'w') as json_file:
  json.dump(n_dict, json_file)

【讨论】:

  • 我测试了它,它运行没有错误,但没有输出!没有任何变化或没有创建文件。我正在从路径加载 json 文件,这会是问题吗?比如:jsonFilePath = r'myFile.json'
猜你喜欢
  • 1970-01-01
  • 2021-04-19
  • 1970-01-01
  • 2020-10-30
  • 2021-11-27
  • 1970-01-01
  • 1970-01-01
  • 2016-01-06
  • 1970-01-01
相关资源
最近更新 更多