python关于字典如何格式化地写入文件之中

Posted 徐阶

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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一词)

以上是关于python关于字典如何格式化地写入文件之中的主要内容,如果未能解决你的问题,请参考以下文章

如何在文件中写入 YAML 格式的数据?

解决Python字典dict写入txt文件中文乱码问题

解决Python字典dict写入txt文件中文乱码问题

如何在python中读取和写入字典到外部文件? [复制]

python把字典写入excel之一例

python写入json文件