python 字典有序并寫入json文件過程解析
更新時間:2019年09月30日 14:55:53 作者:NotForget_On_The_Way
這篇文章主要介紹了python 字典有序并寫入json文件過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
大致流程:
- 導包---import collections
- 新建一個有序字典---collections.OrderedDict()
- 寫入json文件
代碼:
import collections real_result = collections.OrderedDict() real_result["target"] = "total_result" real_result["key1"] = "value1" real_result["key2"] = "value2" real_result["key3"] = "value3" real_result_total = [real_result] print real_result_total 輸出:[OrderedDict([('target', 'total_result'), ('key1', 'value1'), ('key2', 'value2'), ('key3', 'value3')])] current_dir = os.path.dirname(os.path.realpath(__file__)) path = os.path.join(os.path.join(current_dir, 'jsonfile'), result_json) with open(path, 'w') as f: json.dump(real_result_total, f, encoding="utf-8", ensure_ascii=False, indent=4, separators=(',', ':'))
json文件:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
python網(wǎng)絡編程學習筆記(三):socket網(wǎng)絡服務器
服務器和客戶端程序很類似,上節(jié)學習了客戶端程序,這一節(jié)將仔細學習一下利用socket建立TCP服務器和UDP服務器2014-06-06對Python subprocess.Popen子進程管道阻塞詳解
今天小編就為大家分享一篇對Python subprocess.Popen子進程管道阻塞詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10python+opencv邊緣提取與各函數(shù)參數(shù)解析
這篇文章主要介紹了python+opencv邊緣提取與各函數(shù)參數(shù)解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-03-03python生成多個只含0,1元素的隨機數(shù)組或列表的實例
今天小編就為大家分享一篇python生成多個只含0,1元素的隨機數(shù)組或列表的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11