[英]Using a python api that stores a dict in a list of dicts…with no key values
The python API (gmusicapi) stores playlists as a list of dicts with the track info as a dict inside that dict. python API(gmusicapi)将播放列表存储为词典列表,而曲目信息作为该词典中的词典存储。
-edit- this is wrong. -编辑-这是错误的。 it does have some sort of key when printed, but I cant find out how to access the keys within the dict. 它在打印时确实具有某种密钥,但是我无法找到如何在dict中访问密钥。
list = [
{ ##this dict isn't a problem, I can loop through the list and access this.
'playlistId': '0xH6NMfw94',
'name': 'my playlist!',
{'trackId': '02985fhao','album': 'pooooop'}, #this dict is a problem because it has no key name. I need it for track info
'owner': 'Bob'
},
{ ##this dict isn't a problem, I can loop through the list and access this.
'playlistId': '2xHfwucnw77',
'name': 'Workout',
'track':{'trackId': '0uiwaf','album': 'ROOOCKKK'}, #this dict would probably work
'owner': 'Bob'
}
]
I have tried using for loops and accessing it through somethings like: 我试过使用for循环,并通过类似以下方式访问它:
def playLists(self):
print 'attempting to retrieve playlist song info.'
playListTemp = api.get_all_user_playlist_contents()
for x in range(len(playListTemp)):
tempdictionary = dict(playListTemp[x])
The problem here is tempdictionary has a dict in it called tracks but I can't seem to access the keys/value pairs inside it no matter what I do. 这里的问题是,tempdictionary中有一个称为track的字典,但是无论我做什么,我似乎都无法访问其中的键/值对。
when printed it returns something like: 打印时返回如下内容:
[u'kind', u'name', u'deleted', u'creationTimestamp', u'lastModifiedTimestamp', u'recentTimestamp', u'shareToken', 'tracks', u'ownerProfilePhotoUrl', u'ownerName', u'accessControlled', u'type', u'id', u'description']
where 'tracks' is a dict containing artist, title, tracknumber etc 其中“ tracks”是包含艺术家,标题,曲目号等的字典
I also tried something like: 我也尝试过类似的方法:
tempdictionary['tracks'][x]['title'] with no luck. tempdictionary ['tracks'] [x] ['title']没有运气。 Other times I have tried creating a new dict with tracks dict as a velue but then I get an error saying it needs a value of 2 and it found something like 11 etc. 其他时候,我尝试创建一个新的dict并将它作为音轨,但是随后我得到一个错误,说它需要一个2的值,并且发现了11等。
im new to python so if anyone here could help with this I would be very thankful 我是python的新手,所以如果有人可以帮助我,我将非常感激
it does have some sort of key when printed, but I cant find out how to access the keys within the dict. 它在打印时确实具有某种密钥,但是我无法找到如何在dict中访问密钥。
Iterate over the dict: 遍历该字典:
for key in dct:
print(key)
# or do any number of other things with key
If you'll also be looking at the values of the dict, use .items()
to save yourself a dict lookup: 如果您还将查看dict的值,请使用.items()
来保存自己的dict查找:
for key, value in dct.items():
print(key)
print(value)
You might consider using classes to encapsulate common traits. 您可能会考虑使用类来封装常见特征。 Currently, each of your track and playlist dictionaries have a lot of duplicate code (ie. "track_id=", "owner="Bob"). Using classes reduces duplicate and makes your meaning more obvious and explicit. 当前,您的每个曲目和播放列表词典都有很多重复的代码(即“ track_id =”,“ owner =” Bob“)。使用类可以减少重复,并使您的意思更加明显和明确。
class AudioTrack(object):
def __init__(self, ID, album=None):
self.id = ID
self.album = album
self.owner = 'Bob'
Create a single AudioTrack objects like this: 创建单个AudioTrack对象,如下所示:
your_first_track = AudioTrack('02985fhao', 'pooooop')
Or create a list of AudioTrack objects like this: 或创建如下的AudioTrack对象列表:
your_tracks = [
AudioTrack("0x1", album="Rubber Soul"),
AudioTrack("0x2", album="Kind of Blue"),
...
]
In this way, you could inspect each AudioTrack object: 这样,您可以检查每个AudioTrack对象:
your_first_track.id #Returns '02985fhao'
Or do something for all AudioTrack objects in your_tracks: 或对your_tracks中的所有AudioTrack对象执行以下操作:
#Prints the album of every track in the list of AudioTrack intances
for track in your_tracks:
print track.album
You might make playlists using dictionaries where: 您可以使用以下字典来创建播放列表:
my_playlist = {
id: "0x1",
name: "my playlist",
tracks: [AudioTrack("0x1", album="Rubber Soul"),
AudioTrack("0x2", album="Kind of Blue")]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.