aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'lib-python/3/json/tool.py')
-rw-r--r--lib-python/3/json/tool.py23
1 files changed, 13 insertions, 10 deletions
diff --git a/lib-python/3/json/tool.py b/lib-python/3/json/tool.py
index 6d6558acaf..ecf9c47885 100644
--- a/lib-python/3/json/tool.py
+++ b/lib-python/3/json/tool.py
@@ -7,7 +7,7 @@ Usage::
"json": "obj"
}
$ echo '{ 1.2:3.4}' | python -m json.tool
- Expecting property name: line 1 column 2 (char 2)
+ Expecting property name enclosed in double quotes: line 1 column 3 (char 2)
"""
import sys
@@ -18,19 +18,22 @@ def main():
infile = sys.stdin
outfile = sys.stdout
elif len(sys.argv) == 2:
- infile = open(sys.argv[1], 'rb')
+ infile = open(sys.argv[1], 'r')
outfile = sys.stdout
elif len(sys.argv) == 3:
- infile = open(sys.argv[1], 'rb')
- outfile = open(sys.argv[2], 'wb')
+ infile = open(sys.argv[1], 'r')
+ outfile = open(sys.argv[2], 'w')
else:
raise SystemExit(sys.argv[0] + " [infile [outfile]]")
- try:
- obj = json.load(infile)
- except ValueError as e:
- raise SystemExit(e)
- json.dump(obj, outfile, sort_keys=True, indent=4)
- outfile.write('\n')
+ with infile:
+ try:
+ obj = json.load(infile)
+ except ValueError as e:
+ raise SystemExit(e)
+ with outfile:
+ json.dump(obj, outfile, sort_keys=True,
+ indent=4, separators=(',', ': '))
+ outfile.write('\n')
if __name__ == '__main__':