【问题标题】:arranging the json file base on key value根据键值排列json文件
【发布时间】:2020-11-12 17:13:57
【问题描述】:

我在以下格式的文件中有 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
}
}

#2nd 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”键的值对其进行排序。并将其写入一个新文件中。我使用了下面的代码。

    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

【问题讨论】:

    标签: python json sorting


    【解决方案1】:

    您只需转储已排序的 json 列表,并且在使用上下文管理器时无需关闭文件对象

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

    或者如果你想让 jsons 在换行符中

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

    【讨论】:

    • 谢谢。你能告诉我如何在同一个文件中而不是创建一个新文件。
    • 覆盖文件。给出相同的文件名。它会覆盖文件
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多