[BUG] Trailing spaces in pretty-printed JSON
Use this script on a json file and observe all the trailing spaces generated. (screenshot attached.) #!/usr/bin/env python """ Pretty print json file. """ if __name__ == '__main__': import sys import json if '-h' in sys.argv or '--help' in sys.argv: print "Usage: ppjson <FILE>" exit(0) assert sys.argv[1], "No file provided" with open(sys.argv[1]) as f: print json.dumps(json.load(f), indent=4)
On Sat, Oct 13, 2012 at 5:40 PM, Leo
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
On 2012-10-14 02:06 +0800, Xavier Morel wrote:
1. Why didn't you report that on the tracker?
Reported: http://bugs.python.org/issue16476
participants (3)
-
Chris Angelico
-
Leo
-
Xavier Morel