[英]How do I format JSON's with multiple of the same keys with python?
我有一個程序,現在使用powershell腳本和WMI來獲取溫度和負載等數據。 它將數據輸出為JSON文件。 現在讓我先說這是我第一次使用JSON並且我對JSON python庫不太熟悉。 這是我的程序的代碼:
import subprocess
import json
p = subprocess.Popen(["C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe", ". \"./TestScript\";", "&NSV"], stdout=subprocess.PIPE)
(output, err) = p.communicate()
data = json.loads(output)
for mNull in data:
del mNull['Scope']
del mNull['Path']
del mNull['Options']
del mNull['ClassPath']
del mNull['Properties']
del mNull['SystemProperties']
del mNull['Qualifiers']
del mNull['Site']
del mNull['Container']
del mNull['PSComputerName']
del mNull['__GENUS']
del mNull['__CLASS']
del mNull['__SUPERCLASS']
del mNull['__DYNASTY']
del mNull['__RELPATH']
del mNull['__PROPERTY_COUNT']
del mNull['__DERIVATION']
del mNull['__SERVER']
del mNull['__NAMESPACE']
del mNull['__PATH']
fdata = json.dumps(data,indent=2)
print(fdata)
現在這里是生成的JSON:
[
{
"Name": "Memory",
"SensorType": "Load",
"Value": 53.3276978
},
{
"Name": "CPU Core #2",
"SensorType": "Temperature",
"Value": 69
},
{
"Name": "Used Space",
"SensorType": "Load",
"Value": 93.12801
},
{
"Name": "CPU Core #1",
"SensorType": "Temperature",
"Value": 66
},
{
"Name": "CPU DRAM",
"SensorType": "Power",
"Value": 1.05141532
},
{
"Name": "CPU Core #2",
"SensorType": "Load",
"Value": 60.15625
},
{
"Name": "CPU Package",
"SensorType": "Power",
"Value": 15.2162886
},
{
"Name": "Bus Speed",
"SensorType": "Clock",
"Value": 100.000031
},
{
"Name": "CPU Total",
"SensorType": "Load",
"Value": 57.421875
},
{
"Name": "CPU Package",
"SensorType": "Temperature",
"Value": 69
},
{
"Name": "CPU Core #2",
"SensorType": "Clock",
"Value": 2700.00073
},
{
"Name": "Temperature",
"SensorType": "Temperature",
"Value": 41
},
{
"Name": "Used Memory",
"SensorType": "Data",
"Value": 4.215393
},
{
"Name": "Available Memory",
"SensorType": "Data",
"Value": 3.68930435
},
{
"Name": "CPU Core #1",
"SensorType": "Clock",
"Value": 3100.001
},
{
"Name": "CPU Cores",
"SensorType": "Power",
"Value": 13.3746643
},
{
"Name": "CPU Graphics",
"SensorType": "Power",
"Value": 0.119861834
},
{
"Name": "CPU Core #1",
"SensorType": "Load",
"Value": 54.6875
}
]
如您所見,列表中的每個字典都有Name
, SensorType
和Value
。
我想要做的是使每個列表的“標簽”等於每個列表中的Name
,因此我可以一次調用一個特定條目的數據。 再一次,我是JSON及其庫的新手,所以我甚至不確定這種事情是否可行。 任何幫助將不勝感激! 祝你有美好的一天! :)
編輯1:這是一個例子,使用前兩個,我希望程序能夠輸出。
[
"Memory":{
"SensorType": "Load",
"Value": 53.3276978
},
"CPU Core #2":{
"SensorType": "Temperature",
"Value": 69
}
]
再一次,我甚至不知道這是否是有效的JSON,但我希望它只是做一些至少類似的事情,所以我可以調用,例如, print(data["Memory"]["Value"])
並返回, 53.3276978
。
編輯2:我確實發現有一些具有多種傳感器類型的名稱,例如, "CPU Core #1"
和"CPU Core #2"
都具有"Tempurature"
, "Load"
和"Clock"
。 使用上面的例子可能會導致一些沖突,那么有什么方法可以解釋這個問題?
假設您需要保留值,如果密鑰已存在:
import json
data = [
{
"Name": "Memory",
"SensorType": "Load",
"Value": 53.3276978
},
{
"Name": "CPU Core #2",
"SensorType": "Temperature",
"Value": 69
},
{
"Name": "Used Space",
"SensorType": "Load",
"Value": 93.12801
},
{
"Name": "CPU Core #1",
"SensorType": "Temperature",
"Value": 66
},
{
"Name": "CPU DRAM",
"SensorType": "Power",
"Value": 1.05141532
},
{
"Name": "CPU Core #2",
"SensorType": "Load",
"Value": 60.15625
},
{
"Name": "CPU Package",
"SensorType": "Power",
"Value": 15.2162886
},
{
"Name": "Bus Speed",
"SensorType": "Clock",
"Value": 100.000031
},
{
"Name": "CPU Total",
"SensorType": "Load",
"Value": 57.421875
},
{
"Name": "CPU Package",
"SensorType": "Temperature",
"Value": 69
},
{
"Name": "CPU Core #2",
"SensorType": "Clock",
"Value": 2700.00073
},
{
"Name": "Temperature",
"SensorType": "Temperature",
"Value": 41
},
{
"Name": "Used Memory",
"SensorType": "Data",
"Value": 4.215393
},
{
"Name": "Available Memory",
"SensorType": "Data",
"Value": 3.68930435
},
{
"Name": "CPU Core #1",
"SensorType": "Clock",
"Value": 3100.001
},
{
"Name": "CPU Cores",
"SensorType": "Power",
"Value": 13.3746643
},
{
"Name": "CPU Graphics",
"SensorType": "Power",
"Value": 0.119861834
},
{
"Name": "CPU Core #1",
"SensorType": "Load",
"Value": 54.6875
}
]
final_data = {}
for d in data:
if d['Name'] not in final_data:
final_data[d['Name']] = list()
key = d.pop('Name')
final_data[key].append(temp)
print json.dumps(final_data,indent=4)
會給你
{
"CPU Package": [
{
"SensorType": "Power",
"Value": 15.2162886
},
{
"SensorType": "Temperature",
"Value": 69
}
],
"Temperature": [
{
"SensorType": "Temperature",
"Value": 41
}
],
"CPU Core #2": [
{
"SensorType": "Temperature",
"Value": 69
},
{
"SensorType": "Load",
"Value": 60.15625
},
{
"SensorType": "Clock",
"Value": 2700.00073
}
],
"CPU Core #1": [
{
"SensorType": "Temperature",
"Value": 66
},
{
"SensorType": "Clock",
"Value": 3100.001
},
{
"SensorType": "Load",
"Value": 54.6875
}
],
"CPU Cores": [
{
"SensorType": "Power",
"Value": 13.3746643
}
],
"Available Memory": [
{
"SensorType": "Data",
"Value": 3.68930435
}
],
"Used Space": [
{
"SensorType": "Load",
"Value": 93.12801
}
],
"Bus Speed": [
{
"SensorType": "Clock",
"Value": 100.000031
}
],
"Memory": [
{
"SensorType": "Load",
"Value": 53.3276978
}
],
"Used Memory": [
{
"SensorType": "Data",
"Value": 4.215393
}
],
"CPU Total": [
{
"SensorType": "Load",
"Value": 57.421875
}
],
"CPU DRAM": [
{
"SensorType": "Power",
"Value": 1.05141532
}
],
"CPU Graphics": [
{
"SensorType": "Power",
"Value": 0.119861834
}
]
}
希望這可以幫助
你可以像這樣建立一個你想要的形狀的新字典:
...
data = {
element["Name"]: {
key: value for key, value in element.items() if key != "Name"
}
for element in json.loads(output)
}
fdata = json.dumps(data, indent=4)
...
結果:
{
"Memory": {
"SensorType": "Load",
"Value": 53.3276978
},
"CPU Core #2": {
"SensorType": "Clock",
"Value": 2700.00073
},
(and so on)
}
x="""[
{
"Name": "Memory 1",
"SensorType": "Load",
"Value": 53.3276978
},
{
"Name": "CPU Core #2",
"SensorType": "Load",
"Value": 53.3276978
}]"""
json_obj=json.loads(x)
new_list=[]
for item in json_obj:
name=item.pop('Name')
new_list.append({name:item})
print(json.dumps(new_list,indent=4))
產量
[
{
"Memory 1": {
"SensorType": "Load",
"Value": 53.3276978
}
},
{
"CPU Core #2": {
"SensorType": "Load",
"Value": 53.3276978
}
}
]
這個怎么樣?
import json
orig_list = json.load(<filename>)
new_dict = { l['Name']:{k:v for k,v in l.items() if k!='Name'} for l in orig_list}
json.dumps(new_dict, <filename>)
這樣,您就不必從文件中加載的dict
del
項目。
你可以替換
for mNull in data:
del mNull['Scope']
del mNull['Path']
del mNull['Options']
del mNull['ClassPath']
del mNull['Properties']
del mNull['SystemProperties']
del mNull['Qualifiers']
del mNull['Site']
del mNull['Container']
del mNull['PSComputerName']
del mNull['__GENUS']
del mNull['__CLASS']
del mNull['__SUPERCLASS']
del mNull['__DYNASTY']
del mNull['__RELPATH']
del mNull['__PROPERTY_COUNT']
del mNull['__DERIVATION']
del mNull['__SERVER']
del mNull['__NAMESPACE']
del mNull['__PATH']
fdata = json.dumps(data,indent=2)
同
dontwant=set(['Name', 'PSComputerName', '__RELPATH', '__DYNASTY', '__CLASS', '__PROPERTY_COUNT', 'Site', 'ClassPath', 'SystemProperties', 'Scope', 'Qualifiers', 'Options', '__NAMESPACE', 'Path', '__SUPERCLASS', '__DERIVATION', '__GENUS', '__PATH', 'Container', 'Properties', '__SERVER']) # set of keys to drop
out={} # empty dict
for mNull in data:
name=mNull['Name']
out[name]={key:value for key,value in mNull.items() if key not in dontwant} # only copy over items you want
fdata = json.dumps(out,indent=2)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.