import json
处理JSON文件通常涉及读取、解析和写入JSON数据的过程。下面是一些在Python中处理JSON文件的常用方法:
1. 读取JSON文件:
使用Python内置的json模块可以轻松读取JSON文件。首先,需要导入json模块并打开文件,然后使用json.load()方法将JSON数据加载到Python对象中。
import json
with open('example.json') as file:
data = json.load(file)
1. 解析JSON数据:
一旦将JSON数据加载到Python对象中,就可以使用Python的语法对其进行处理。例如,可以访问嵌套的字典、列表和键值对等。
# 访问嵌套的字典
name = data['person']['name']
age = data['person']['age']
# 访问列表
scores = data['scores']
score_1 = scores[0]
# 遍历嵌套的字典和列表
for student in data['students']:
print(student['name'])
print(student['age'])
print(student['scores'])
1. 修改JSON数据:
可以根据需要对JSON数据进行修改。可以修改特定的键值对,添加新的键值对或删除现有的键值对。这里演示如何添加新的键值对。
data['person']['email'] = '[email protected]'
1. 写入JSON文件:
使用json.dump()方法将修改后的Python对象写入JSON文件。同样需要使用Python内置的open()函数打开文件,指定写入模式,并使用json.dump()方法将数据写入文件。
with open('output.json', 'w') as file:
json.dump(data, file)
这些是处理JSON文件的基本步骤。根据你的具体需求,可能需要更复杂的逻辑来处理更复杂的JSON结构。
实战:
假设我有下面这份fio的json结果文档,叫做fio.json好了,我对其进行解析,抓我需要的write的IOPS和BW的结果
{
"fio version" : "fio-3.1",
"timestamp" : 1696925985,
"timestamp_ms" : 1696925985515,
"time" : "Tue Oct 10 16:19:45 2023",
"jobs" : [
{
"jobname" : "128k_write_mix0_num1_iodepth64",
"groupid" : 0,
"error" : 0,
"eta" : 0,
"elapsed" : 301,
"job options" : {
"name" : "128k_write_mix0_num1_iodepth64",
"filename" : "/dev/nvme0n1",
"ioengine" : "libaio",
"direct" : "1",
"rw" : "write",
"rwmixread" : "0",
"bs" : "128k",
"numjobs" : "1",
"iodepth" : "64",
"runtime" : "300",
"time_based" : "1",
"write_iops_log" : "./1010/128k_write_mix0_num1_iodepth64",
"write_bw_log" : "./1010/128k_write_mix0_num1_iodepth64",
"log_avg_msec" : "1000"
},
"read" : {
"io_bytes" : 0,
"io_kbytes" : 0,
"bw" : 0,
"iops" : 0.000000,
"runtime" : 0,
"total_ios" : 0,
"short_ios" : 0,
"drop_ios" : 0,
"slat_ns" : {
"min" : 0,
"max" : 0,
"mean" : 0.000000,
"stddev" : 0.000000
},
"clat_ns" : {
"min" : 0,
"max" : 0,
"mean" : 0.000000,
"stddev" : 0.000000,
"percentile" : {
"1.000000" : 0,
"5.000000" : 0,
"10.000000" : 0,
"20.000000" : 0,
"30.000000" : 0,
"40.000000" : 0,
"50.000000" : 0,
"60.000000" : 0,
"70.000000" : 0,
"80.000000" : 0,
"90.000000" : 0,
"95.000000" : 0,
"99.000000" : 0,
"99.500000" : 0,
"99.900000" : 0,
"99.950000" : 0,
"99.990000" : 0,
"0.00" : 0,
"0.00" : 0,
"0.00" : 0
}
},
"lat_ns" : {
"min" : 0,
"max" : 0,
"mean" : 0.000000,
"stddev" : 0.000000
},
"bw_min" : 0,
"bw_max" : 0,
"bw_agg" : 0.000000,
"bw_mean" : 0.000000,
"bw_dev" : 0.000000,
"bw_samples" : 0,
"iops_min" : 0,
"iops_max" : 0,
"iops_mean" : 0.000000,
"iops_stddev" : 0.000000,
"iops_samples" : 0
},
"write" : {
"io_bytes" : 995677306880,
"io_kbytes" : 972341120,
"bw" : 3241104,
"iops" : 25321.130122,
"runtime" : 300003,
"total_ios" : 7596415,
"short_ios" : 0,
"drop_ios" : 0,
"slat_ns" : {
"min" : 3449,
"max" : 120791,
"mean" : 9509.510472,
"stddev" : 4055.313047
},
"clat_ns" : {
"min" : 275173,
"max" : 26670061,
"mean" : 2517367.006916,
"stddev" : 1031195.720264,
"percentile" : {
"1.000000" : 1056768,
"5.000000" : 1253376,
"10.000000" : 1368064,
"20.000000" : 1630208,
"30.000000" : 1892352,
"40.000000" : 2146304,
"50.000000" : 2408448,
"60.000000" : 2670592,
"70.000000" : 2867200,
"80.000000" : 3129344,
"90.000000" : 3817472,
"95.000000" : 4358144,
"99.000000" : 5734400,
"99.500000" : 6455296,
"99.900000" : 8454144,
"99.950000" : 9371648,
"99.990000" : 15269888,
"0.00" : 0,
"0.00" : 0,
"0.00" : 0
}
},
"lat_ns" : {
"min" : 279136,
"max" : 26675989,
"mean" : 2526944.836070,
"stddev" : 1031563.153543
},
"bw_min" : 1596160,
"bw_max" : 3427584,
"bw_agg" : 99.997880,
"bw_mean" : 3241035.300000,
"bw_dev" : 309378.872013,
"bw_samples" : 300,
"iops_min" : 12470,
"iops_max" : 26778,
"iops_mean" : 25320.580000,
"iops_stddev" : 2417.018326,
"iops_samples" : 300
},
"trim" : {
"io_bytes" : 0,
"io_kbytes" : 0,
"bw" : 0,
"iops" : 0.000000,
"runtime" : 0,
"total_ios" : 0,
"short_ios" : 0,
"drop_ios" : 0,
"slat_ns" : {
"min" : 0,
"max" : 0,
"mean" : 0.000000,
"stddev" : 0.000000
},
"clat_ns" : {
"min" : 0,
"max" : 0,
"mean" : 0.000000,
"stddev" : 0.000000,
"percentile" : {
"1.000000" : 0,
"5.000000" : 0,
"10.000000" : 0,
"20.000000" : 0,
"30.000000" : 0,
"40.000000" : 0,
"50.000000" : 0,
"60.000000" : 0,
"70.000000" : 0,
"80.000000" : 0,
"90.000000" : 0,
"95.000000" : 0,
"99.000000" : 0,
"99.500000" : 0,
"99.900000" : 0,
"99.950000" : 0,
"99.990000" : 0,
"0.00" : 0,
"0.00" : 0,
"0.00" : 0
}
},
"lat_ns" : {
"min" : 0,
"max" : 0,
"mean" : 0.000000,
"stddev" : 0.000000
},
"bw_min" : 0,
"bw_max" : 0,
"bw_agg" : 0.000000,
"bw_mean" : 0.000000,
"bw_dev" : 0.000000,
"bw_samples" : 0,
"iops_min" : 0,
"iops_max" : 0,
"iops_mean" : 0.000000,
"iops_stddev" : 0.000000,
"iops_samples" : 0
},
"usr_cpu" : 16.971887,
"sys_cpu" : 16.700555,
"ctx" : 3797071,
"majf" : 0,
"minf" : 10,
"iodepth_level" : {
"1" : 0.100000,
"2" : 0.100000,
"4" : 0.100000,
"8" : 0.100000,
"16" : 0.100000,
"32" : 0.100000,
">=64" : 99.999171
},
"latency_ns" : {
"2" : 0.000000,
"4" : 0.000000,
"10" : 0.000000,
"20" : 0.000000,
"50" : 0.000000,
"100" : 0.000000,
"250" : 0.000000,
"500" : 0.000000,
"750" : 0.000000,
"1000" : 0.000000
},
"latency_us" : {
"2" : 0.000000,
"4" : 0.000000,
"10" : 0.000000,
"20" : 0.000000,
"50" : 0.000000,
"100" : 0.000000,
"250" : 0.000000,
"500" : 0.010000,
"750" : 0.032186,
"1000" : 0.438352
},
"latency_ms" : {
"2" : 34.327943,
"4" : 57.133003,
"10" : 8.031209,
"20" : 0.030922,
"50" : 0.010000,
"100" : 0.000000,
"250" : 0.000000,
"500" : 0.000000,
"750" : 0.000000,
"1000" : 0.000000,
"2000" : 0.000000,
">=2000" : 0.000000
},
"latency_depth" : 64,
"latency_target" : 0,
"latency_percentile" : 100.000000,
"latency_window" : 0
}
],
"disk_util" : [
{
"name" : "nvme0n1",
"read_ios" : 44,
"write_ios" : 7593512,
"read_merges" : 0,
"write_merges" : 0,
"read_ticks" : 4,
"write_ticks" : 19039100,
"in_queue" : 18805644,
"util" : 100.000000
}
]
}
with open('fio.json') as file:
data = json.load(file)
#这里的data['jobs'][0]代表取字典jobs的值后,是一个[],看作为List,需要取list里面的值,我们这里只有一个,取[0],取到以后再取key ['write'],在这个字典下取key['iops'],则可以拿到对应的iops的值
iops = data['jobs'][0]['write']['iops']
bw = data['jobs'][0]['write']['bw']
这里我们最后 iops和bw的值分别为:25321.130122 和 3241104
感兴趣可以一个一个试,看输出的是什么:
即打印
print(data['jobs'])
print(data['jobs'][0])
以此类推,观察每一步有啥输出,有助理解