I've parsed a JSON file containing Track Names and Album Names of about 3000 songs, and was getting nowhere with trying to organise it into a list of lists or something similar?
I essentially want to sort the items into a list of unique albums, each containing a list of tracks, and was wondering if this was actually possible, as I'm don't know how to assigned names to lists?
My code so far is as follows:
import json from pprint import pprint with open('.../playlist.json', encoding="utf-8") as f: data = json.load(f) for playlist in data['playlists']: if playlist['name'] == 'PLAYLIST3': for item in playlist['items']: track = item['track']['trackName'] album = item['track']['albumName']
Also, the songs aren't sorted in any particular manner.
EDIT: This is what I did, which seemed to work fine in creating a list of dictionaries with a list of songs as the value and the album name as the key:
import json from pprint import pprint with open('C:/Users/nambi/Desktop/parse/playlist.json', encoding="utf-8") as f: data = json.load(f) listOfAlbums = {} for playlist in data['playlists']: if playlist['name'] == 'P4': for item in playlist['items']: title = item['track']['trackName'] album = item['track']['albumName'] if album in listOfAlbums: listOfAlbums[album].append(title) else: listOfAlbums[album] = [title]
https://stackoverflow.com/questions/66501253/organising-results-from-json-file-using-python-lists March 06, 2021 at 08:35AM
没有评论:
发表评论