-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson
More file actions
executable file
·41 lines (30 loc) · 843 Bytes
/
Copy pathjson
File metadata and controls
executable file
·41 lines (30 loc) · 843 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Time-stamp: <2012-01-11 19:31:12 Wednesday by taoshanwen>
# @version 1.0
# @author ahei
import sys
import json
def toUtf8(s):
"""s也可能是int等类型."""
if isinstance(s, unicode):
return s.encode("utf8", "ignore")
return str(s)
def prettyObject(o, args = {"indent":4, "ensure_ascii":False}):
return toUtf8(json.dumps(o, **args))
def main():
if len(sys.argv) == 1:
infile = sys.stdin
outfile = sys.stdout
elif len(sys.argv) == 2:
infile = open(sys.argv[1], 'rb')
outfile = sys.stdout
else:
raise SystemExit(sys.argv[0] + " [infile]")
try:
obj = json.load(infile)
except ValueError, e:
raise SystemExit(e)
print prettyObject(obj)
if __name__ == '__main__':
main()