Python 詞典(Dict) 加載與保存示例
更新時間:2019年12月06日 08:59:05 作者:瘋狂的小豬oO
今天小編就為大家分享一篇Python 詞典(Dict) 加載與保存示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
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 詞典(Dict) 加載與保存示例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關(guān)文章
用python介紹4種常用的單鏈表翻轉(zhuǎn)的方法小結(jié)
這篇文章主要介紹了用python介紹4種常用的單鏈表翻轉(zhuǎn)的方法小結(jié),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02python多項式擬合之np.polyfit 和 np.polyld詳解
這篇文章主要介紹了python多項式擬合之np.polyfit 和 np.polyld的實例代碼,python數(shù)據(jù)擬合主要可采用numpy庫,庫的安裝可直接用pip install numpy等,需要的朋友跟隨小編一起學(xué)習(xí)吧2020-02-02