1

我正在使用 Discogs API 获取数据,并且我想将 JSON 限制为仅具有“CD”或“专辑”或“Maxi-Single”格式的记录。我不确定如何过滤获取的数据,因为 Discogs 没有更简单的方法来从初始请求本身过滤它。您会注意到下面提到的 JSON URL 包含我想省略的“DVD”和“VHS”。任何想法/线索在获取完成后如何过滤它?

JSON URL:http ://api.discogs.com/database/search?sort=year&sort_order=asc&artist=Britney+Spears&track=Crazy+Stop+Remix&type=master

Python

url = 'http://api.discogs.com/database/search?sort=year&sort_order=asc&artist=Britney+Spears&track=Crazy+Stop+Remix&type=master'
request = urllib2.Request(url)
request.add_header('User-Agent','Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)')
request.add_header('Content-Type','application/json')
response = urllib2.urlopen(request)
json_object = json.load(response)
if json_object['results'] == []:
    print 'No results found'
else:
    json_object_results_by_year=[x for x in json_object['results'] if 'year' in x]
    json_sorted_results = sorted(json_object_results_by_year,key=lambda x:x['year'])
    #print json_sorted_results
4

1 回答 1

3
wanted = {u'CD',u'Album',u'Maxi-Single'}
[i for i in json_object[u'results'] if any(w in wanted for w in i[u'format'])]

或者

[i for i in json_object[u'results'] if any(w in i[u'format'] for w in wanted)]
于 2013-01-27T13:55:57.823 回答