程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

python processing json file problem

編輯:Python
Problem and background

Want to process a json file.There are many objects in this result. At present, the order of these objects is messy. I want to sort these objects according to the value of start, from small to large.

import jsonimport operatorwith open('lab2G05.json','r',encoding='utf8')as fp: json_datas = json.load(fp)for json_data in json_datas: for item in json_data["annotations"][0]["result"]: start = item["value"]["start"] # print(start) # result is a list, and now I want to sort this list.How to get result in result?sort_start = sorted(result, key=operator.itemgetter(start), reverse=False) # I want to sort these items in result according to start from small to large.But now it's in result, I don't know how to get result, and then according to start to sort
Running result and error content
sort_start = sorted(result, key=operator.itemgetter(start), reverse=False)

NameError: name 'result' is not defined

My solution ideas and tried methods

Transfer result to item
Get this error

 sort_start = sorted(item, key=operator.itemgetter(start), reverse=False)IndexError: string index out of range
My desired outcome

These objects in the result in the source file can be sorted according to the start value from small to large.


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved