Aug-28-2020, 07:38 AM
Hi,
When you launch an API call to the imdb (movie database) e.g. to retrieve details about a film (e.g. 'TOPKAPI'),
your response is an elaborate dictionary where dictionaries are mixed with lists etc..
In order to "pretty print" this mess, i constructed the code below, which works fine.
But, i feel that this is a bespoke solution for this site. Other responses may be constructed differently.
Is there a more elegant solution?
Paul
When you launch an API call to the imdb (movie database) e.g. to retrieve details about a film (e.g. 'TOPKAPI'),
your response is an elaborate dictionary where dictionaries are mixed with lists etc..
In order to "pretty print" this mess, i constructed the code below, which works fine.
But, i feel that this is a bespoke solution for this site. Other responses may be constructed differently.
Is there a more elegant solution?
response = requests.request("GET", url, headers=headers) dictfilm = response.json() def prettyStr(k,v): print(f'{k} \t : {v}') def prettyDict(x): for k,v in x.items(): print(f'{k} \t : {v}') def prettyList(x): print(f'{x[0]} \t : {x[1]}') for k,v in dictfilm.items(): if type(v) == str: prettyStr(k,v) elif type(v) == dict: prettyDict(v) elif type(v) == list: for element in v: if type(element) == dict: prettyDict(element) elif type(element) == list: prettyList(element) else: print('You forgot this piece') else: print('You forgot this piece')thx,
Paul
Output:{'id': 'tt0058672', 'title': 'Topkapi\xa0', 'year': '1964', 'length': '2h', 'rating': '7.0', 'rating_votes': '7904', 'poster': 'https://m.media-amazon.com/images/M/MV5BMTM2MTcyNDQ4Nl5BMl5BanBnXkFtZTcwMzQxNDY3NA@@.jpg', 'plot': 'A conman gets mixed up with a group of thieves who plan to rob an Istanbul museum to steal a jewelled dagger.', 'trailer': {'id': 'vi1705313561', 'link': 'https://www.imdb.com/videoplayer/vi1705313561'}, 'cast': [{'actor': 'Melina Mercouri', 'actor_id': 'nm0580479', 'character': 'Elizabeth Lipp'}, {'actor': 'Peter Ustinov', 'actor_id': 'nm0001811', 'character': 'Arthur Simon Simpson'}, {'actor': 'Maximilian Schell', 'actor_id': 'nm0001703', 'character': 'Walter Harper'}, {'actor': 'Robert Morley', 'actor_id': 'nm0605923', 'character': 'Cedric Page'}, {'actor': 'Jess Hahn', 'actor_id': 'nm0353916', 'character': 'Hans Fisher'}, {'actor': 'Gilles Ségal', 'actor_id': 'nm0845329', 'character': 'Giulio the Human Fly'}, {'actor': 'Akim Tamiroff', 'actor_id': 'nm0848667', 'character': 'Gerven - the Cook'}, {'actor': 'Titos Vandis', 'actor_id': 'nm0888937', 'character': 'Harback (as Titos Wandis)'}, {'actor': 'Ege Ernart', 'actor_id': 'nm0259623', 'character': 'Maj. Ali Tufan'}, {'actor': 'Senih Orkan', 'actor_id': 'nm0649901', 'character': 'First Shadow'}, {'actor': 'Ahmet Danyal Topatan', 'actor_id': 'nm0867626', 'character': 'Second Shadow'}, {'actor': 'Joe Dassin', 'actor_id': 'nm0202087', 'character': 'Josef'}, {'actor': 'Despo Diamantidou', 'actor_id': 'nm0221615', 'character': 'Voula'}], 'technical_specs': [['Runtime', '2 hr (120 min)'], ['Sound Mix', 'Mono (Westrex Recording System)'], ['Color', 'Color (Technicolor)'], ['Aspect Ratio', '1.66 : 1 (intended ratio)'], ['Laboratory', 'Laboratoires Franay Tirages Cinematographiques (LTC), Paris, France (as Franay L.T.C. St. Cloud)'], ['Negative Format', '35 mm'], ['Cinematographic Process', 'Spherical'], ['Printed Film Format', '35 mm']]}