python sort list of json by value

前端 未结 4 1062
北海茫月
北海茫月 2020-12-28 12:33

I have a file consists of JSON, each a line, and want to sort the file by update_time reversed.

sample JSON file:

{ \"page\": { \"url\": \"url1\", \"         


        
4条回答
  •  一生所求
    2020-12-28 13:13

    You can use dict.get() with a default value:

    lines = sorted(lines, key=lambda k: k['page'].get('update_time', 0), reverse=True)
    

    Example:

    >>> lines = [
    ...     {"page": {"url": "url1", "update_time": "1415387875"}, "other_key": {}},
    ...     {"page": {"url": "url2", "update_time": "1415381963"}, "other_key": {}},
    ...     {"page": {"url": "url3", "update_time": "1415384938"}, "other_key": {}},
    ...     {"page": {"url": "url4"}, "other_key": {}},
    ...     {"page": {"url": "url5"}, "other_key": {}}
    ... ]
    >>> lines = sorted(lines, key=lambda k: k['page'].get('update_time', 0), reverse=True)
    >>> for line in lines:
    ...     print line
    ... 
    {'other_key': {}, 'page': {'url': 'url1', 'update_time': '1415387875'}}
    {'other_key': {}, 'page': {'url': 'url3', 'update_time': '1415384938'}}
    {'other_key': {}, 'page': {'url': 'url2', 'update_time': '1415381963'}}
    {'other_key': {}, 'page': {'url': 'url4'}}
    {'other_key': {}, 'page': {'url': 'url5'}}
    

    Though, I would still follow the EAFP principle that Ferdinand suggested - this way you would also handle cases when page key is also missing. Much easier to let it fail and handle it than checking all sorts of corner cases.

提交回复
热议问题