0

I am learning Python (teaching myself through articles and tutorials) and have come across something that I need assistance with. I have JSON data that represents TV listings, and I want to read it, edit it (remove outdated listings), and rewrite it back out. The sticking points for me are the nested data and how to iterate through/reference it to skip over the objects I don't want when writing out. Thanks. Here is some sample data:

{
  "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"
  }
}

This is the code I have tried:

import json


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)
Martin Gergov
  • 1,556
  • 4
  • 20
  • 29
PigskinsX
  • 3
  • 2
  • Maybe you just need [json library](https://docs.python.org/3/library/json.html#module-json)? – RedEyed Sep 11 '17 at 13:39
  • added my code attempt – PigskinsX Sep 11 '17 at 15:28
  • It's also a good idea to describe specifically how your code is failing. In this case I'm guessing it's running successfully but failing to delete the desired elements? As such, this appears to be more or less a duplicate of [Remove items from a list while iterating](https://stackoverflow.com/questions/1207406/remove-items-from-a-list-while-iterating). – glibdud Sep 11 '17 at 15:43
  • Yes, that is the case. It runs but fails to delete any elements. I reviewed the article you linked and while it makes sense logically, my limited knowledge of Python is the barrier. When I try to make a copy of the item I am iterating, I get "unhashable type". I understand why that happens but am clueless as to what to do next. – PigskinsX Sep 11 '17 at 17:01

1 Answers1

0

Given how del works, that's not too surprising. You want to remove an item from the list of items, not "delete a declared variable from the current execution scope". So instead of del e2, remove e2 from element['items'], using remove and friends, e.g.:

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)
Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153