1

我正在尝试按键的值(年份)对 JSON 数据进行排序,然后尝试仅显示格式为 'DVD' 的项目,我尝试了很多方法,但无法得出一个干净的解决方案。你能帮我解决这个问题吗?我是python新手,如果在这里问得太简单,请原谅。

JSON:

{
    "pagination": {
        "per_page": 50,
        "pages": 1,
        "page": 1,
        "urls": {},
        "items": 8
    },
    "results": [{
        "style": ["RnB/Swing", "Ballad"],
        "thumb": "http://api.discogs.com/image/R-90-217365-1270055830.jpeg",
        "title": "Britney Spears - Oops!...I Did It Again",
        "country": "US",
        "format": ["CD", "Album"],
        "uri": "/Britney-Spears-OopsI-Did-It-Again/master/27443",
        "label": ["Jive"],
        "catno": "01241-41704-2",
        "year": "2000",
        "genre": ["Pop"],
        "resource_url": "http://api.discogs.com/masters/27443",
        "type": "master",
        "id": 27443
    }, {
        "thumb": "http://api.discogs.com/image/R-90-2029929-1259535792.jpeg",
        "title": "Britney Spears - The Singles Collection",
        "country": "UK",
        "format": ["CD", "Compilation"],
        "uri": "/Britney-Spears-The-Singles-Collection/master/203074",
        "label": ["Jive"],
        "catno": "88697 623422",
        "year": "2009",
        "genre": ["Pop"],
        "resource_url": "http://api.discogs.com/masters/203074",
        "type": "master",
        "id": 203074
    }, {
        "style": ["RnB/Swing"],
        "thumb": "http://api.discogs.com/image/R-90-1895804-1250792689.jpeg",
        "title": "Britney Spears - Greatest Hits: My Prerogative",
        "country": "US",
        "format": ["DVD", "Compilation", "NTSC"],
        "uri": "/Britney-Spears-Greatest-Hits-My-Prerogative/master/270902",
        "label": ["Jive", "Zomba Video"],
        "catno": "82876-65443-9A",
        "year": "2004",
        "genre": ["Electronic", "Pop"],
        "resource_url": "http://api.discogs.com/masters/270902",
        "type": "master",
        "id": 270902
    }, {
        "style": ["RnB/Swing", "Ballad"],
        "thumb": "http://api.discogs.com/image/R-90-2421214-1283141143.jpeg",
        "format": ["DVD", "DVD-Video", "NTSC", "Multichannel"],
        "country": "US",
        "barcode": ["0 1241-41784-9 7"],
        "uri": "/Britney-Spears-Live-From-Las-Vegas/master/271031",
        "label": ["Jive"],
        "catno": "01241-41784-9",
        "year": "2002",
        "genre": ["Electronic", "Hip Hop", "Pop"],
        "title": "Britney Spears - Live From Las Vegas",
        "resource_url": "http://api.discogs.com/masters/271031",
        "type": "master",
        "id": 271031
    }, {
        "style": ["Europop"],
        "thumb": "http://api.discogs.com/image/R-90-2410806-1283122933.jpeg",
        "format": ["DVD", "DVD-Video", "NTSC"],
        "country": "US",
        "barcode": ["0 1241-41704-9 1"],
        "uri": "/Britney-Spears-Live-And-More/master/270904",
        "label": ["Jive"],
        "catno": "01241-41704-9",
        "year": "2000",
        "genre": ["Pop"],
        "title": "Britney Spears - Live And More!",
        "resource_url": "http://api.discogs.com/masters/270904",
        "type": "master",
        "id": 270904
    }]
}

PYTHON:

url1 = "http://api.discogs.com/database/search?sort=year&sort_order="
url2 = "asc&artist=%22"+artist+"%22&track="
url3 = "%22"+song+"%22&format_exact=Album&type=master"
url = url1 + url2 + url3
print url
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_raw = response.readlines()
json_object = json.loads(json_raw[0])

for row in sorted(json_object['results']): #stuck here to sort on key's value...
    print '-----------------------------start------------------------------------------'
    print row['format']
    print row['id']
    print '-----------------------------end--------------------------------------------'
4

1 回答 1

2

sorted接受一个key参数,它就是这样做的:

from operator import itemgetter

for result in sorted(json_object['results'], key=itemgetter('year')):
    if 'DVD' in result['format']:
        continue
于 2013-01-19T21:22:08.130 回答