python關於字典如何格式化地寫入文件之中


1.python關於字典如何式化地寫入文件之中

  如何寫入:https://blog.csdn.net/qq_15642411/article/details/79943741

    (推薦使用json包)

  參看:https://blog.csdn.net/lly1122334/article/details/102858447?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

    (如何一行一個項目輸出)

  可以將字典對象變為json的格式,再使用json模塊的dumps函數(注意設置indent參數)

  這里推薦一個學習這些模塊和函數方法的好方法,就是按  F12 鍵查看模塊的源代碼或者說明文檔,雖然是英文,但是很原汁原味。

  (學會通過看 源代碼和官方文檔 來學習)

  example:json模塊dumps方法的indent參數

 
         
         If ``indent`` is a non-negative /非負的 integer/ 整數, then JSON array(數組) elements and
       object members will be pretty-printed(格式輸出) with that indent level. An indent
       level of 0 will only insert newlines(換行符). ``None`` is the most compact(合同,契約)
               representation
 
 關於原文中為什么要用數組一此,可以參考這一篇文章,講解json到底是什么
  json格式:
    對象表示為鍵值對
    數據由逗號分隔
    花括號保存對象
    方括號保存數組(前面所講的為什么要用array一詞)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM