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中if __name__ == ''__main__''
今天小編就為大家分享一篇關(guān)于正確理解Python中if __name__ == '__main__' ,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-01-01
Pygame Surface創(chuàng)建圖像的實現(xiàn)
本文主要介紹了Pygame Surface創(chuàng)建圖像的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02
Python從MySQL數(shù)據(jù)庫中面抽取試題,生成試卷
這篇文章主要介紹了Python如何從MySQL數(shù)據(jù)庫中面抽取試題,生成試卷,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2021-01-01
Python 如何用一行代碼實現(xiàn)for循環(huán)初始化數(shù)組
這篇文章主要介紹了Python 用一行代碼實現(xiàn)for循環(huán)初始化數(shù)組的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03

