Python json.dumps参数indent

起因:
最近公司要求写一个爬虫,需要将爬取下来的文件存为json进行对比存档,然而保存下来的文件不尽人意,从而用到了indent这个参数,好奇心驱使对值的范围不同有什么不一样,而在网上寻找许久没有一个标准答案,只能开始翻看官方文档。

官方文档内容:

If indent is a non-negative integer or string, then JSON array elements and object members will be pretty-printed with that indent level. An indent level of 0, negative, or “” will only insert newlines. None (the default) selects the most compact representation. Using a positive integer indent indents that many spaces per level. If indent is a string (such as “\t”), that string is used to indent each level.

简单的翻译就是:

如果indent是非负整数或字符串,则JSON数组元素和对象成员将使用该缩进级别进行漂亮打印。 缩进级别0,负数或“”只会插入换行符。 无(默认)选择最紧凑的表示形式。 使用正整数缩进会使每个级别缩进多个空格。 如果缩进是字符串(例如“ \ t”),则该字符串用于缩进每个级别。

这里可以很清晰的看出,如果参数为正整数就将其进行漂亮的格式化打印,如果是复数和空值、默认的情况下就是执行紧凑型打印,如果是字符串用于缩进每个几倍。

你可能感兴趣的:(Python)