欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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文件:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論