0

I've been trying to parse a dictionary, which is returned by a database I'm working on, but I'm not sure about the best approach to take. I think the difficulty is being caused by the fact that the list sizes within the dictionary are not symmetrical, so my approach doesn't seem to be able to pull out what I'm looking for.

The data structure looks like this:

a = {1: [(u'Old Town Road', u'Lil Nas X featuring Billy Ray Cyrus')], 2: [], 3: [(u'Talk', u'Coldplay'), (u'Talk', u'Khalid')], 4: [(u'Sucker', u'Jonas Brothers')], 5: [(u"I Don't Care", u'Buck Owens'), (u"I Don't Care", u'Fallout Boy'), (u"I Don't Care", u'Justin Bieber')], 6: [], 7: [(u'Sunflower (Spider-Man: Into the Spider-Verse)', u'Post Malone & Swae Lee')], 8: [], 9: [], 10: [(u'Dancing With A Stranger', u'Sam Smith')]

and when I've done something like:

a[1][0][0]

I can get back the title 'Old Town Road' and when I try something like:

a[1][0][1]

I'm able to get back the artist 'Lil Nas X featuring Billy Ray Cyrus'

Here are the important structures of the code:

top100 = {1: {'artist': 'Lil Nas X Featuring Billy Ray Cyrus', 'title': 'Old Town Road'}, 2: {'artist': 'Billie Eilish', 'title': 'Bad Guy'}, 3: {'artist': 'Khalid', 'title': 'Talk'}, 4: {'artist': 'Jonas Brothers', 'title': 'Sucker'}, 5: {'artist': 'Ed Sheeran & Justin Bieber', 'title': "I Don't Care"}, 6: {'artist': 'Post Malone', 'title': 'Wow.'}, 7: {'artist': 'Post Malone & Swae Lee', 'title': 'Sunflower (Spider-Man: Into The Spider-Verse)'}, 8: {'artist': 'DaBaby', 'title': 'Suge'}, 9: {'artist': 'Chris Brown Featuring Drake', 'title': 'No Guidance'}, 10: {'artist': 'Sam Smith & Normani', 'title': 'Dancing With A Stranger'}, 11: {'artist': 'Polo G Featuring Lil Tjay', 'title': 'Pop Out'}, 12: {'artist': 'Shawn Mendes', 'title': "If I Can't Have You"}, 13: {'artist': 'Ava Max', 'title': 'Sweet But Psycho'}, 14: {'artist': 'Taylor Swift Featuring Brendon Urie', 'title': 'ME!'}, 15: {'artist': 'Halsey', 'title': 'Without Me'}, 16: {'artist': 'Ariana Grande', 'title': '7 Rings'}, 17: {'artist': 'Lizzo', 'title': 'Truth Hurts'}, 18: {'artist': 'Marshmello & Bastille', 'title': 'Happier'}, 19: {'artist': 'Blake Shelton', 'title': "God's Country"}, 20: {'artist': 'Morgan Wallen', 'title': 'Whiskey Glasses'}, 21: {'artist': 'Panic! At The Disco', 'title': 'High Hopes'}, 22: {'artist': 'Luke Combs', 'title': 'Beer Never Broke My Heart'}, 23: {'artist': 'Daddy Yankee & Katy Perry Featuring Snow', 'title': 'Con Calma'}, 24: {'artist': 'Young Thug, J. Cole & Travis Scott', 'title': 'The London'}, 25: {'artist': 'J. Cole', 'title': 'Middle Child'}, 26: {'artist': 'City Girls', 'title': 'Act Up'}, 27: {'artist': 'benny blanco, Halsey & Khalid', 'title': 'Eastside'}, 28: {'artist': 'Katy Perry', 'title': 'Never Really Over'}, 29: {'artist': 'Mustard & Migos', 'title': 'Pure Water'}, 30: {'artist': 'Tyler, The Creator', 'title': 'Earfquake'}, 31: {'artist': 'Panic! At The Disco', 'title': 'Hey Look Ma, I Made It'}, 32: {'artist': 'Meek Mill Featuring Drake', 'title': 'Going Bad'}, 33: {'artist': 'Dan + Shay', 'title': 'Speechless'}, 34: {'artist': 'Lady Gaga & Bradley Cooper', 'title': 'Shallow'}, 35: {'artist': 'Khalid', 'title': 'Better'}, 36: {'artist': 'Lee Brice', 'title': 'Rumor'}, 37: {'artist': 'Ariana Grande', 'title': "Break Up With Your Girlfriend, I'm Bored"}, 38: {'artist': 'Travis Scott', 'title': 'Sicko Mode'}, 39: {'artist': 'Thomas Rhett', 'title': 'Look What God Gave Her'}, 40: {'artist': 'A Boogie Wit da Hoodie', 'title': 'Look Back At It'}, 41: {'artist': 'Calboy', 'title': 'Envy Me'}, 42: {'artist': 'Billie Eilish', 'title': "When The Party's Over"}, 43: {'artist': 'Halsey', 'title': 'Nightmare'}, 44: {'artist': 'Jonas Brothers', 'title': 'Cool'}, 45: {'artist': 'Luke Combs', 'title': 'Beautiful Crazy'}, 46: {'artist': 'Kane Brown', 'title': 'Good As You'}, 47: {'artist': 'Cardi B', 'title': 'Press'}, 48: {'artist': 'Lil Baby', 'title': 'Close Friends'}, 49: {'artist': 'Ed Sheeran Featuring Chance The Rapper & PnB Rock', 'title': 'Cross Me'}, 50: {'artist': 'YG, Tyga & Jon Z', 'title': 'Go Loko'}, 51: {'artist': 'Cardi B & Bruno Mars', 'title': 'Please Me'}, 52: {'artist': 'Brett Eldredge', 'title': 'Love Someone'}, 53: {'artist': 'Offset Featuring Cardi B', 'title': 'Clout'}, 54: {'artist': 'YK Osiris', 'title': 'Worth It'}, 55: {'artist': 'Lewis Capaldi', 'title': 'Someone You Loved'}, 56: {'artist': 'Kelsea Ballerini', 'title': 'Miss Me More'}, 57: {'artist': 'P!nk', 'title': 'Walk Me Home'}, 58: {'artist': 'Billie Eilish', 'title': 'Bury A Friend'}, 59: {'artist': 'Maren Morris', 'title': 'GIRL'}, 60: {'artist': 'DJ Khaled Featuring SZA', 'title': 'Just Us'}, 61: {'artist': 'Luke Bryan', 'title': "Knockin' Boots"}, 62: {'artist': 'Luke Combs', 'title': "Even Though I'm Leaving"}, 63: {'artist': '5 Seconds Of Summer', 'title': 'Easier'}, 64: {'artist': 'Summer Walker X Drake', 'title': 'Girls Need Love'}, 65: {'artist': 'Lil Tecca', 'title': 'Ran$om'}, 66: {'artist': 'Blanco Brown', 'title': 'The Git Up'}, 67: {'artist': 'Meek Mill Featuring Ella Mai', 'title': '24/7'}, 68: {'artist': 'Jason Aldean', 'title': 'Rearview Town'}, 69: {'artist': 'Bad Bunny & Tainy', 'title': 'Callaita'}, 70: {'artist': 'DJ Khaled Featuring Cardi B & 21 Savage', 'title': 'Wish Wish'}, 71: {'artist': 'Dan + Shay', 'title': 'All To Myself'}, 72: {'artist': 'Chase Rice', 'title': 'Eyes On You'}, 73: {'artist': 'Beyonce', 'title': 'Before I Let Go'}, 74: {'artist': 'Eric Church', 'title': 'Some Of It'}, 75: {'artist': 'Marshmello Featuring CHVRCHES', 'title': 'Here With Me'}, 76: {'artist': 'Lil Uzi Vert', 'title': 'Sanguine Paradise'}, 77: {'artist': 'Lunay, Daddy Yankee & Bad Bunny', 'title': 'Soltera'}, 78: {'artist': 'Florida Georgia Line', 'title': 'Talk You Out Of It'}, 79: {'artist': 'Yo Gotti Featuring Lil Baby', 'title': 'Put A Date On It'}, 80: {'artist': 'Eli Young Band', 'title': "Love Ain't"}, 81: {'artist': 'NLE Choppa', 'title': 'Shotta Flow'}, 82: {'artist': 'Pedro Capo X Farruko', 'title': 'Calma'}, 83: {'artist': 'Avicii', 'title': 'Heaven'}, 84: {'artist': 'The Chainsmokers & Bebe Rexha', 'title': 'Call You Mine'}, 85: {'artist': 'Billie Eilish', 'title': 'Ocean Eyes'}, 86: {'artist': 'Megan Thee Stallion', 'title': 'Big Ole Freak'}, 87: {'artist': 'Future', 'title': 'Please Tell Me'}, 88: {'artist': 'Cody Johnson', 'title': 'On My Way To You'}, 89: {'artist': 'SHAED', 'title': 'Trampoline'}, 90: {'artist': 'Chris Young', 'title': 'Raised On Country'}, 91: {'artist': 'Nicky Jam X Ozuna', 'title': 'Te Robare'}, 92: {'artist': 'Ozuna', 'title': 'Amor Genuino'}, 93: {'artist': 'Jonas Brothers', 'title': 'Only Human'}, 94: {'artist': 'Yella Beezy, Gucci Mane & Quavo', 'title': 'Bacc At It Again'}, 95: {'artist': 'Bryce Vine Featuring YG', 'title': 'La La Land'}, 96: {'artist': 'Juice WRLD', 'title': 'Robbery'}, 97: {'artist': 'Ozuna x Daddy Yankee x J Balvin x Farruko x Anuel AA', 'title': 'Baila Baila Baila'}, 98: {'artist': 'Future', 'title': 'XanaX Damage'}, 99: {'artist': 'Future', 'title': 'Government Official'}, 100: {'artist': 'Sech Featuring Darell', 'title': 'Otro Trago'}}

def read(conn, query):
    """Executes a Query against the specified connection and query params"""
    cursor = conn.cursor()
    cursor.execute(query)
    data = cursor.fetchall()
    print(data)
    return data


def top100Search(conn, top100):
    """Executes a query for the values in the top100 variable against the database specified within conn param and returns back a dictionary"""
    results = {}
    for items in top100: 
        topquery = "SELECT Title, Performer FROM Media WHERE Title ='" + top100[items]['title'].replace("'","''") + "'"
        temp = read(conn, topquery)
        results.update({items:temp})
    return results

a = top100Search(conn, top100)

I've tried this, based on the a[0][0][1] idea, but I'm misunderstanding how this slicing works:

for items, keys, values in a:
     try: 
        print(a[items][keys][values])
     except:
         print[a[items]]

But I end up getting back the error "TypeError: 'int' object is not iterable"

The code makes a connection to an Azure Database, runs a specified query, and returns back a dictionary, but I don't know how to parse what is being outputted and I'm wondering if I should even try to parse what is output or restructure the output from the top100Search function?

Likewise, as mentioned above, my approach to the for loop is not effective and is throwing the error:

"TypeError: 'int' object is not iterable"

The question has been marked as a duplicate, but I don't feel like the answer of 'how to parse a dictionary' is complete. We are parsing a dictionary, that contains within it lists, which then has within it a tuple and only in some cases.

ParanoidPenguin
  • 151
  • 1
  • 13

1 Answers1

0

looks like a JSON structure is being returned. I would say you should use a python JSON parser liek this

import json

# some JSON:
x =  '{ "name":"John", "age":30, "city":"New York"}'

# parse x:
y = json.loads(x)

# the result is a Python dictionary:
print(y["age"]) 
HAL9000
  • 95
  • 8