我正尝试在python中读取json文件,并且想将长值分成多行以使其更易于阅读。
例如JSON文件:
mem
的值太长,因此我想使其多行。
{
"resource":{
"cpu": ["host, usage_system, usage_user, usage_iowait", "1=1"],
"mem": ["host, active/(1024*1024) as active, available/(1024*1024) as available, available_percent, buffered/(1024*1024) as buffered, cached/(1024*1024) as cached, swap_cached/(1024*1024) swap_cached, swap_total/(1024*1024) swap_total, total/(1024*1024) mem_total, used/(1024*1024) mem_used", "1=1"],
"net": ["host, bytes_recv, bytes_sent, packets_recv, packets_sent", "1=1 and interface != 'all'"]
}
}
我想像下面这样设定值。
{ "resource":{
"cpu": ["host, usage_system, usage_user, usage_iowait", "1=1"],
"mem": ["host, active/(1024*1024) as active, available/(1024*1024) as available,
available_percent, buffered/(1024*1024) as buffered,
cached/(1024*1024) as cached, swap_cached/(1024*1024) swap_cached,
swap_total/(1024*1024) swap_total,
total/(1024*1024) mem_total, used/(1024*1024) mem_used", "1=1"],
"net": ["host, bytes_recv, bytes_sent, packets_recv, packets_sent", "1=1 and interface != 'all'"]
}
}
谢谢!
答案 0 :(得分:0)
好的,因此您可能要在json中列出字符串列表,然后将它们与“” .join()组合。例如, json.json
{
"multiline": [
"this is a re",
"ally long st",
"ring, so I n",
"eed to make ",
"it one line"
]
}
multiline.py
import json
with open("json.json", "r") as jsonfile:
stringlist = json.loads(jsonfile.read())["multiline"]
result = "".join(stringlist)
print(result)