我在文件中有一系列类似的值。如何根据一个键对文件中的对象进行单独排序
{
"canceled": false,
"complete_time": "2017-06-08T15:55:45.616942",
"create_time": "2017-06-08T15:55:44.370344",
"entity_list": [
{
"entity_type": 2,
"uuid": "xxxxx"
},
{
"entity_name": "",
"uuid": "xxxx"
}
],
"last_updated_time": "2017-06-08T15:55:45.616942",
"progress_status": 3,
"request": {
"arg": {
"parent_task_uuid": "xxx",
"task_uuid": "xxxx",
"transition": 2,
"vm_uuid": "xxx"
},
"method_name": """""
},
"response": {
"error_code": 0,
"error_detail": "",
"ret": {}
},
"start_time": "2017-06-08T15:55:44.452703",
"uuid": "xxxxx"
}
{
"canceled": false,
"complete_time": "2017-06-08T15:55:45.616942",
"create_time": "2017-06-08T15:55:44.370344",
"entity_list": [
{
"entity_type": 2,
"uuid": "xxxxx"
},
{
"entity_name": "",
"uuid": "xxxx"
}
],
"last_updated_time": "2017-06-08T15:55:45.616942",
"progress_status": 3,
"request": {
"arg": {
"parent_task_uuid": "xxx",
"task_uuid": "xxxx",
"transition": 2,
"vm_uuid": "xxx"
},
"method_name": """""
},
"response": {
"error_code": 0,
"error_detail": "",
"ret": {}
},
"start_time": "2017-06-08T15:55:44.452703",
"uuid": "xxxxx"
}
我想根据'last_updated_field'对这些单独的块进行排序{}。如果它像一个JSON,我已经编写了代码在Python中工作,但由于这不是一个有效的JSON,我怎么能做到这一点。
while True:
line = sys.stdin.readline()
if not line: break
line = line.strip()
json_obj = json.loads(line)
lines.append(json_obj)
lines = sorted(lines, key=lambda k: k['last_updated_time'], reverse=True)
你必须在你的文件这样的吗?对象和列表中括号之间没有逗号? –
是的。对象和括号之间没有逗号。 – HighlyCurious
关于这种可怕格式的* only *正面的事情是JSON不允许'} {'在字符串之外的组合,并且您可以通过添加括号并执行正则表达式来替代'} \ s * {'→'},{'。 –