How to save a list of python dicts into a json file with vertical display
本问题已经有最佳答案,请猛点这里访问。
我想将python dicts a的列表保存到json文件b中。
1 | json.dump(A, B) |
这样做。但是保存的JSON文件的格式是
['a':1,'b':1,'a':2,'b':2…]
我想要的是显示如下:
1 2 3 4 5 | [ {'a': 1, 'b': 1}, {'a': 2, 'b': 2}, ... ], |
以便其他人可以轻松阅读。有办法吗?
当使用
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.
1 2 3 4 5 | >>> print(json.dumps({1:'a', 2: 'b'}, indent=1)) { "1":"a", "2":"b" } |