查看JSON数据
import json
# 探索数据的结构
filename = 'eq_data_1_day_m1.geojson'
with open(filename) as f:
all_eq_data = json.load(f)
readable_file = 'readable_eq_data.json'
with open(readable_file, 'w') as f:
json.dump(all_eq_data, f, indent=4)
json.load()
将数据转换为Python能够处理的格式 - - 字典
json.dump()
接受一个JSON数据对象和一个文件对象,并将数据写入这个文件中
indent=4 让dump() 使用与数据结构匹配的缩进量来设置数据的格式
创建地震列表
创建一个列表,包含所有地震的各种信息
all_eq_dicts = all_eq_data['features']
len(all_eq_dicts) # 160 # 160次地震
提取震级
magnitude震级
mags = []# 存储地震震级
for eq_dict in all_eq_dicts:
mag = eq_dict['properties']['mag']
mags.append(mag)
print(mags[:10]) # [1.6, 1.6, 2.2, 3.7, 2.92000008, 1.4, 4.6, 4.5, 1.9, 1.8]