CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
csv2json.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import sys
4 import optparse
5 import re
6 from FWCore.PythonUtilities.LumiList import LumiList
7 
8 
9 if __name__ == '__main__':
10 
11  parser = optparse.OptionParser ("Usage: %prog input.csv")
12  parser.add_option ('--output', dest='output', type='string',
13  help='Save output to file OUTPUT')
14  parser.add_option ('--runIndex', dest='runIndex', type='int',
15  default = 0,
16  help='column to be converted to run number (default %default)')
17  parser.add_option ('--lumiIndex', dest='lumiIndex', type='int',
18  default = 1,
19  help='column to be converted to lumi section number (default %default)')
20  # required parameters
21  (options, args) = parser.parse_args()
22  if len (args) != 1:
23  raise RuntimeError, "Must provide exactly one input file"
24 
25  sepRE = re.compile (r'[\s,;:]+')
26  runLumiDict = {}
27  events = open (args[0], 'r')
28  runIndex, lumiIndex = options.runIndex, options.lumiIndex
29  minPieces = max (runIndex, lumiIndex) + 1
30  for line in events:
31  pieces = sepRE.split (line.strip())
32  if len (pieces) < minPieces:
33  continue
34  try:
35  run, lumi = int( pieces[runIndex] ), int( pieces[lumiIndex] )
36  except:
37  continue
38  runLumiDict.setdefault (run, []).append (lumi)
39  jsonList = LumiList (runsAndLumis = runLumiDict)
40  if options.output:
41  jsonList.writeJSON (options.output)
42  else:
43  print jsonList