在Python中读取,编辑,编写嵌套的JSON数据

时间:2017-09-11 13:35:38

标签: python json

我正在学习Python(通过文章和教程自学)并且遇到了一些我需要帮助的东西。我有代表电视节目单的JSON数据,我想阅读它,编辑它(删除过时的列表),然后重新将其重新编写。对我来说,关键点是嵌套数据以及如何迭代/引用它以跳过我在写出时不想要的对象。谢谢。以下是一些示例数据:

{
  "1": {
    "channel_id": "1",
    "img": "https://guide.tv/assets/images/channels/1.png",
    "items": [
      {
        "category": "Comedy",
        "channel": "1",
        "description": "Latest episode of show A",
        "end_time": "2017-09-11 20:30:00",
        "language": "us",
        "name": "Show A",
        "quality": "720p",
        "runtime": "30",
        "time": "2017-09-11 20:00:00",
        "version": "None"
      },
      {
        "category": "Comedy",
        "channel": "1",
        "description": "Latest episode of show B",
        "end_time": "2017-09-12 21:00:00",
        "language": "us",
        "name": "Show B",
        "quality": "720p",
        "runtime": "30",
        "time": "2017-09-12 20:30:00",
        "version": "None"
      },
    ],
    "name": "01 - NBC"
  },
  "2": {
    "channel_id": "2",
    "img": "https://guide.tv/assets/images/channels/2.png",
    "items": [
      {
        "category": "Drama",
        "channel": "2",
        "description": "Latest episode of show C",
        "end_time": "2017-09-10 23:00:00",
        "language": "us",
        "name": "Show C",
        "quality": "720p",
        "runtime": "180",
        "time": "2017-09-10 20:00:00",
        "version": "None"
      },
      {
        "category": "Drama",
        "channel": "2",
        "description": "Latest episode of show D",
        "end_time": "2017-09-11 23:00:00",
        "language": "us",
        "name": "Show D",
        "quality": "720p",
        "runtime": "60",
        "time": "2017-09-11 22:00:00",
        "version": "None"
      },
      {
        "category": "Action",
        "channel": "2",
        "description": "Latest episode of Show E",
        "end_time": "2017-09-11 22:00:00",
        "language": "us",
        "name": "Show E",
        "quality": "720p",
        "runtime": "180",
        "time": "2017-09-11 19:00:00",
        "version": "None"
      },
      {
        "category": "Fiction",
        "channel": "2",
        "description": "Latest episode of show F",
        "end_time": "2017-09-10 19:00:00",
        "language": "us",
        "name": "Show F",
        "quality": "720p",
        "runtime": "180",
        "time": "2017-09-10 16:00:00",
        "version": "None"
      },
    ],
    "name": "02 - CBS"
  },
  "3": {
    "channel_id": "3",
    "img": "https://guide.tv/assets/images/channels/3.png",
    "items": [
      {
        "category": "Comedy",
        "channel": "3",
        "description": "Latest episode of show G",
        "end_time": "2017-09-18 12:00:00",
        "language": "us",
        "name": "Show G",
        "quality": "hqlq",
        "runtime": "120",
        "time": "2017-09-18 10:00:00",
        "version": "None"
      },
      {
        "category": "Action",
        "channel": "3",
        "description": "Latest episode of show H",
        "end_time": "2017-09-19 12:00:00",
        "language": "us",
        "name": "Show H",
        "quality": "hqlq",
        "runtime": "120",
        "time": "2017-09-19 10:00:00",
        "version": "None"
      },
    ],
    "name": "03 - ABC"
  }
}

这是我尝试过的代码:

with open('file.json') as data_file:
    data = json.load(data_file)
for element in data.values():
    if 'items' in element:
        for e2 in element['items']:
            if '2017-09-10' in e2['time']:
                del e2
print json.dumps(data, indent=4, sort_keys=True)

1 个答案:

答案 0 :(得分:0)

鉴于del的工作原理,这并不奇怪。您要从项目列表中删除项目,而不是“从当前执行范围中删除已声明的变量”。因此,使用remove and friendsdel e2中删除e2,而不是element['items'],例如:

elements = data.values()
for element in elements:
  if 'items' in element:
    listing = element['items']
    for entry in listing:
      if 'time' in entry and '2017-09-10' in entry['time']:
        listing.remove(entry)