正在尝试解析数据库返回的数据结构,但不确定最佳的解析方法

2024-09-28 23:40:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试解析一个字典,它是由我正在处理的数据库返回的,但我不确定最好的方法是什么。我认为困难是因为字典中的列表大小是不对称的,所以我的方法似乎无法找出我想要的

数据结构如下所示:

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')]

当我做了类似的事情:

a[1][0][0]

当我尝试以下方式时,我可以重新获得“老城路”的名称:

a[1][0][1]

我能找回由比利·雷·塞勒斯主演的艺术家《Lil Nas X》

以下是守则的重要结构:

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)

基于[0][0][1]的想法,我尝试过这种方法,但我误解了这种切片的工作原理:

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

但我最终得到了错误“TypeError:'int'对象不可编辑”

代码连接到Azure数据库,运行指定的查询,并返回一个字典,但我不知道如何解析输出的内容,我想知道是否应该尝试解析输出内容或重新构造top100Search函数的输出

同样,如上所述,我对for循环的方法无效,并且抛出了错误:

“TypeError:'int'对象不可编辑”

这个问题被标记为重复,但我觉得“如何解析词典”的答案并不完整。我们正在解析一个字典,其中包含列表,然后在其中包含一个元组,并且仅在某些情况下


Tags: the方法you字典titleartistitemsit
1条回答
网友
1楼 · 发布于 2024-09-28 23:40:42

看起来正在返回JSON结构。我想说的是,您应该使用python JSON解析器

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"]) 

相关问题 更多 >