[Python-Dev] [BUG] Trailing spaces in pretty-printed JSON (original) (raw)

Chris Angelico rosuav at gmail.com
Sat Oct 13 20:08:31 CEST 2012


On Sat, Oct 13, 2012 at 5:40 PM, Leo <sdl.web at gmail.com> wrote:

Use this script on a json file and observe all the trailing spaces generated. (screenshot attached.)

Confirmed as still the case in Python 3 (specifically, with an early alpha of 3.3 and with 3.2 for Windows). It's because the item separator is '; ' and the newline and indent are appended to that. That can be overridden with:

json.dumps({"asdf":"123","qwer":"234","zxcv":"345"},indent=4,separators=(',',': '))

but that compacts everything, not sure if that's what you want.

The code in question is in Lib/json/encoder.py in the source tree; I'm sure you could post on the tracker (hint) with a patch (hint hint) if you want it to strip spaces followed by newlines.

ChrisA



More information about the Python-Dev mailing list