-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprintJSON.py
More file actions
70 lines (52 loc) · 1.38 KB
/
printJSON.py
File metadata and controls
70 lines (52 loc) · 1.38 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/python
#
#------------JSON-------------
title = 'printJSON'
import os, string,sys, json
import pprint
debug=False
def usage():
""" Usage: printJSON file
Reads in JSON file and writes sorted run list to output
"""
pass
def OpenFile(file_in,iodir):
""" file_in -- Input file name
iodir -- 'r' readonly 'r+' read+write """
try:
ifile=open(file_in, iodir)
# print "Opened file: ",file_in," iodir ",iodir
except:
print "Could not open file: ",file_in
sys.exit(1)
return ifile
def CloseFile(ifile):
ifile.close()
if __name__ == "__main__":
narg=len(sys.argv)
if narg < 2 :
print usage.__doc__
sys.exit(1)
inputJSON=sys.argv[1]
infile=OpenFile(inputJSON,'r')
data = json.load(infile)
if debug:
print(json.dumps(data, sort_keys=True, indent=4))
runs=[]
for idat in data:
if debug:
print idat,type(idat)
runnumber=int(idat.encode("ascii"))
runs.append(runnumber)
sortedruns=sorted(runs)
print "\nNumber of runs in JSON file: ",len(runs)
# print runs
if debug:
print sortedruns
outstring="Runs: "
for irun in sortedruns:
outstring =outstring + str(irun) + ", "
print outstring[:-2]
## print data.keys()
## pprint(data)
## CloseFile(infile)