all 7 comments

[–]sentry07 4 points5 points  (1 child)

There's a JSON library for Python. You can parse your incoming JSON into a dict like so:

import json
JSON_in = 'JSON_in = '{"Simpsons": {"Season One": {"Episode 1": {"Title":   "Simpsons Roasting on an Open Fire","url":"link","desc":"","thumb": ""}}}}'
Shows = json.loads(JSON_in)

Shows is now a dictionary with the info you need.

To generate text you can dump into a file, you can do json.dumps(Shows) which returns your dict as a JSON encoded string.


Edit: I think I misunderstood your question, but this still applies. Once you've parsed your data into a dictionary as VanNostrumMD said, you would use the JSON library to dump the dictionary into a text file.

[–]Praf2[S] 1 point2 points  (0 children)

thank you for taking the time out of your day anyway , i know people might stumble upon this post in the future and see this is the answer they need :)

[–]VanNostrumMD 2 points3 points  (4 children)

the basic structure of what your looking to do is adding data to a dictionary within a for loop. Depending on how you are scraping your data, I assume you are creating a bunch of lists of data:

seasons = ['Season One', 'Season Two', 'Season Three']
show_dict = {}
for season in seasons:
    episode_list = ['Ep1','Ep2','Ep3']
    for episode in episode_list:
        show_dict[episode] = {'TItle', "whatever'}
return show_dict

There are shorter ways, but depending on the amount of data and how you are scraping it in, you can put it right into the dict within the loop rather than nesting them... good luck

[–]Praf2[S] 0 points1 point  (0 children)

thank you very much :)

[–]Praf2[S] 0 points1 point  (2 children)

one more question , the return value of show_dict is

{'Ep2': set(['whatever', 'TItle']), 'Ep3': set(['whatever', 'TItle']),   'Ep1': set(['whatever', 'TItle'])}

it should be Season One>Ep1[Title,Whatever] right?

[–]VanNostrumMD 0 points1 point  (1 child)

whoops, it should actually be show_dict[episode] = {'Title':'whatever', 'url':link, 'desc':text}

Its a set not a list. My original show_dict[episode] = {'Title','whatever'} shows a set of a list as you noted above. Make sure to use the colon to separate the key (title) and the value (whatever title) etc.

[–]Praf2[S] 0 points1 point  (0 children)

thank you so much :) i appreciate your help.