Indent behavior of json.dumps is different between python2 and python3

The behavior of json.dumps is different when the indent option is specified between python2.7 and python3.6. In python2, there is one space before the line feed code, but in python3 it is not.

Experiment

code

import json
json.dumps({"name": 'aaa', "age": 21}, sort_keys=True, indent=2)

result

python2.7.8

'{\n  "age": 21, \n  "name": "aaa"\n}'

python3.6.0

> '{\n  "age": 21,\n  "name": "aaa"\n}'

Cause and response

Cause

Issue 16333: Trailing whitespace in json dump when using indent - Python tracker

Countermeasures

Add separators = (',',':') to the options in json.dumps.

import json
json.dumps({"name": 'aaa', "age": 21}, sort_keys=True, indent=2, separators=(',', ': '))

Recommended Posts

Indent behavior of json.dumps is different between python2 and python3
The answer of "1/2" is different between python2 and 3
The behavior of @property is different between class definition and old and new styles
Difference between == and is in python
The timing when the value of the default argument is evaluated is different between Ruby and Python.
About the difference between "==" and "is" in python
[python] behavior of argmax
[Python] Python and security-① What is Python?
Identity and equivalence Python is and ==
Source installation and installation of Python
Python module num2words Difference in behavior between English and Russian
Python a + = b and a = a + b are different
Environment construction of python and opencv
Difference between Ruby and Python split
The story of Python and the story of NaN
Difference between list () and [] in Python
Installation of SciPy and matplotlib (Python)
Manipulability ellipsoid of arm and mobile robot is drawn with python
Cooperation between python module and API
Differences between Python, stftime and strptime
Difference between python2 series and python3 series dict.keys ()
This and that of python properties
Behavior of python3 by Sakura's server
I investigated the behavior of the difference between hard links and symbolic links
[Python] Difference between function and method
Summary of differences between Python and PHP (comparison table of main items)
Coexistence of Python2 and 3 with CircleCI (1.0)
Memo to switch between python2 series and 3 series in anaconda environment of mac (win is also added)
Python --Difference between exec and eval
[Python] Difference between randrange () and randint ()
Summary of Python indexes and slices
[Python] Difference between sorted and sorted (Colaboratory)
Reputation of Python books and reference books
[Comparison of PHP, Ruby, and Python description] For those who are wondering how the description method is different.
[Introduction to Python] What is the difference between a list and a tuple?
I'm stunned by the behavior of filter () due to different versions of Python
[Python] Summary of conversion between character strings and numerical values (ascii code)
Graduate from Python (x, y) and try mixing different versions of Python @Windows
Installation of Visual studio code and installation of python
Communicate between Elixir and Python with gRPC
Differences in authenticity between Python and JavaScript
[Python] What is pandas Series and DataFrame?
Check the behavior of destructor in Python
Differences between Ruby and Python in scope
difference between statements (statements) and expressions (expressions) in Python
python> link> strftime () and strptime () behavior / code
Extraction of tweet.js (json.loads and eval) (Python)
What kind of programming language is Python?
Differences in syntax between Python and Java
Difference between PHP and Python finally and exit
Difference between @classmethod and @staticmethod in Python
Connect a lot of Python or and and
Difference between append and + = in Python list
Difference between nonlocal and global in Python
[Python] Difference between class method and static method
[python] [meta] Is the type of python a type?
[Python3] Switch between Shift_JIS, UTF-8 and ASCII
[Python Iroha] Difference between List and Tuple
Conversion between singular and plural of words
[python] Difference between rand and randn output
Easy introduction of python3 series and OpenCV3