根据键值排列json文件

2024-10-02 16:35:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个文件中的json数据,格式如下。我有1000件pkt(其中2件如下所示)

{
"SensorData": 
{ "LeadStatus": 0, 
"Respiration": [1647240, 1647038, 1646865, 1646807], 
"PatchId": "TUVT1", 
"TsSpO2": 6262999470, 
"SpO2": [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], 
"Accel": [7, 0, -32768, -32768, -32768, -32768, -32768, -32768, -32768, -932, 15201, 5515, -930, 15194, 5514, -938, 15189, 5516, -934, 15195, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
"IAGain": 0, 
"vBat": 2749, 
"Seq": 25053, 
"TsECG": 6263250944, 
"ACCEL": [-32768, -32768, -32768, -32768, -32768, -32768, -32768, -932, 15201, 5515, -930, 15194, 5514, -938, 15189, 5516, -934, 15195, 0, 0, 0], "ECG1": [-41, -47, -11, 27, 64, 103, 141, 183, 189, 145, 105, 63, 22, -19, -56, -47, -27, -8, -2, -4, -3, -3, -4, -3, -3, -3, -3, -2, -3, -2, -3, -3, -2, -3, -2, -2, -1, -1, -1, 0, 1, 0, 2, 2, 3, 3, 3, 5, 6, 7, 8, 8, 10, 10, 11, 14, 16, 17, 18, 19, 21], "ECG0": [-15, -15, 6, 31, 56, 80, 102, 129, 134, 107, 84, 57, 32, 6, -16, -14, -6, -2, -1, 0, -1, -1, -1, -1, 0, -1, 0, 0, -1, 0, 0, -1, -1, 0, 1, 1, 1, 2, 1, 2, 3, 3, 3, 4, 5, 4, 5, 5, 6, 7, 7, 8, 8, 9, 10, 11, 12, 12, 14, 15, 17], "RLDInformation": 0
}
}

#第二个pkt在下一行

{
"SensorData": 
{"LeadStatus": 0, 
"Respiration": [1646840, 1646829, 1646680, 1646472], 
"PatchId": "TUVT1", 
"TsSpO2": 6263249460, 
"SpO2": [255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], 
"Accel": [7, 0, -32768, -32768, -32768, -32768, -32768, -32768, -32768, -936, 15201, 5521, -937, 15202, 5520, -937, 15202, 5520, -931, 15197, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
"IAGain": 0, 
"vBat": 2749, 
"Seq": 25054, 
"TsECG": 6263500800, 
"ACCEL": [-32768, -32768, -32768, -32768, -32768, -32768, -32768, -936, 15201, 5521, -937, 15202, 5520, -937, 15202, 5520, -931, 15197, 0, 0, 0], "ECG1": [22, 24, 27, 30, 34, 36, 38, 42, 44, 42, 41, 38, 37, 37, 36, 36, 34, 27, 20, 12, 7, 4, 1, -2, -4, -3, -3, -4, -2, -4, -3, -3, -3, -3, -3, -3, -4, -3, -3, -3, -2, -3, -4, -3, -2, -3, -3, -3, -3, -2, -3, -3, -3, -2, -3, -3, -3, -3, -3, -3, -3], 
"ECG0": [17, 18, 20, 24, 25, 28, 29, 30, 32, 30, 30, 29, 28, 27, 27, 28, 24, 20, 17, 10, 7, 4, 4, 1, 0, 0, -1, 0, 0, -1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, -1, -1, 0, 0, -2, 0, 0, 0, -1, -1, -1, 0, 0, 0, -1, -1, -1, 0, 0, -1, -1], 
"RLDInformation": 0, 
"Temperature": -32768}}

我想根据'Seq'key.的值对其进行排序,并将其写入一个新文件中。我使用了下面的代码

    import json
    data=[]
    fp = open('RecData_1.json','r')
    for line in fp:
      data.append(json.loads(line))
    fp.close()

    def rev_sorting(json):
      try:
        return (json['SensorData']['Seq'])
      except KeyError:
        return 0
    data.sort(key=rev_sorting, reverse=True)

    with open("sample.json","w") as outfile:
      for line in data:
        json.dump(line,outfile)
    outfile.close()

当我运行代码时,它会创建一个新文件,但不会打开

我从下面的回答中得到了帮助。 How do I write JSON data to a file?python sort list of json by value


Tags: 文件jsondatalineseqoutfilefppkt
1条回答
网友
1楼 · 发布于 2024-10-02 16:35:12

您只需转储已排序的JSON列表,在使用上下文管理器时,不需要关闭文件对象

with open("sample.json","w") as outfile:
      json.dump(data, outfile)

或者如果你想让JSON在新行中

with open("sample.json","w") as outfile:
      for line in data:
          outfile.write(json.dumps(line) + "\n")

相关问题 更多 >