Python 词典(Dict) 加载与保存示例
时间:2021-05-28 08:01:05|栏目:Python代码|点击: 次
Dict的加载:
import json def load_dict(filename): '''load dict from json file''' with open(filename,"r") as json_file: dic = json.load(json_file) return dic
Dict的保存:
import json import datetime import numpy as np class JsonEncoder(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): return obj.__str__() else: return super(MyEncoder, self).default(obj) def save_dict(filename, dic): '''save dict into json file''' with open(filename,'w') as json_file: json.dump(dic, json_file, ensure_ascii=False, cls=JsonEncoder)
上一篇:python右对齐的实例方法
栏 目:Python代码
下一篇:Python 通过requests实现腾讯新闻抓取爬虫的方法
本文地址:http://www.codeinn.net/misctech/130463.html






