我有以下格式的输入数据(Stdout)。有没有办法使用python只提取json格式?
删除行的逻辑,直到我们得到"{“直到结尾"}”,或者从输入变量中提取json格式
输入数据为
stdout="""nameserver 8.8.8.8
Hit:1 http://archive.ubuntu.com/ubuntu xenial InRelease
Hit:2 http://archive.ubuntu.com/ubuntu xenial-updates InRelease
Hit:3 http://security.ubuntu.com/ubuntu xenial-security InRelease
Hit:4 http://archive.ubuntu.com/ubuntu xenial-backports InRelease
Reading package lists...
Reading package lists...
Building dependency tree...
Reading state information...
fio is already the newest version (2.2.10-1ubuntu1).
0 upgraded, 0 newly installed, 0 to remove and 50 not upgraded.
{
"fio version" : "fio-2.2.10",
"timestamp" : 1589874145,
"time" : "Tue May 19 07:42:25 2020",
"jobs" : [
{
"jobname" : "yardstick-fio",
"groupid" : 0,
"error" : 0,
"eta" : 0,
"elapsed" : 31,
"read" : {
"io_bytes" : 11723776,
"bw" : 585954,
"short_ios" : 0,
"drop_ios" : 0,
"slat" : {
"min" : 26,
"max" : 54318,
"mean" : 38.89,
"stddev" : 437.20
},
"latency_ms" : {
"2" : 0.39,
"4" : 0.07,
"250" : 0.01,
"500" : 0.00,
"750" : 0.00,
"1000" : 0.00,
"2000" : 0.00,
">=2000" : 0.00
},
}
],
"disk_util" : [
{
"name" : "vda",
"read_ios" : 30743,
"write_ios" : 26938,
"read_merges" : 0,
"write_merges" : 11,
"read_ticks" : 28652,
"write_ticks" : 29192,
"in_queue" : 57836,
"util" : 96.17
}
]
}"""新变量(New_stdout)中的预期输出:
{
"fio version" : "fio-2.2.10",
"timestamp" : 1589874145,
"time" : "Tue May 19 07:42:25 2020",
"jobs" : [
{
"jobname" : "yardstick-fio",
"groupid" : 0,
"error" : 0,
"eta" : 0,
"elapsed" : 31,
"read" : {
"io_bytes" : 11723776,
"bw" : 585954,
"short_ios" : 0,
"drop_ios" : 0,
"slat" : {
"min" : 26,
"max" : 54318,
"mean" : 38.89,
"stddev" : 437.20
},
"latency_ms" : {
"2" : 0.39,
"4" : 0.07,
"250" : 0.01,
"500" : 0.00,
"750" : 0.00,
"1000" : 0.00,
"2000" : 0.00,
">=2000" : 0.00
},
}
],
"disk_util" : [
{
"name" : "vda",
"read_ios" : 30743,
"write_ios" : 26938,
"read_merges" : 0,
"write_merges" : 11,
"read_ticks" : 28652,
"write_ticks" : 29192,
"in_queue" : 57836,
"util" : 96.17
}
]
}发布于 2020-05-21 01:07:17
如果你确定在第一个之前没有'{‘,在开头有'}’,我会用正则表达式来处理它(如果你有不同的格式,我可以进行调整):
import re
pattern = re.compile(r"{.*}", re.DOTALL)
match = pattern.search(stdout)
print(match.group(0))如果你不喜欢正则表达式,但是你仍然对字符串的结构有相同的假设,你可以这样做:
start = stdout.find('{')
stdout[start:] # it means from start to the endhttps://stackoverflow.com/questions/61917428
复制相似问题