赞
踩
import json
import datetime
class NpEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, np.integer):
return int(obj)
elif isinstance(obj, np.floating):
return float(obj)
elif isinstance(obj, np.ndarray):
return obj.tolist()
elif isinstance(obj, datetime.datetime):
return obj.strftime('%Y-%m-%dT%H:%M:%S')
else:
return super(NpEncoder, self).default(obj)
with open("write_json.json", "w", encoding='utf-8') as f:#读的时候也需要用utf-8
# json.dump(dict_, f) # 写为一行
json.dump(json_dict_2, f, indent=2, sort_keys=True, ensure_ascii=False, cls=NpEncoder) # 写为多行
参考链接
链接、
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。