CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_9/src/RecoLuminosity/LumiDB/python/csvReporter.py

Go to the documentation of this file.
00001 import csv
00002 class csvReporter(object):
00003     def __init__(self,filename,dialect='excel',delimiter=',',quoting=csv.QUOTE_NONNUMERIC):
00004         """input params:
00005         filename : output csv path/name
00006         """
00007         self.__filename=filename
00008         self.__file=open(self.__filename,'wb')
00009         self.__writer=csv.writer(self.__file)
00010     def writeRow(self,row):
00011         try:
00012             self.__writer.writerow(row)
00013         except csv.Error,e:
00014             sys.exit('file %s: %s'%(self.__filename,e))
00015     def writeRows(self,rows):
00016         try:
00017             self.__writer.writerows(rows)
00018         except csv.Error,e:
00019             sys.exit('file %s: %s'%(self.__filename,e))
00020     def close(self):
00021         self.__file.close()
00022 
00023 if __name__ == '__main__':
00024     filename='testwrite.csv'
00025     r=csvReporter(filename,dialect='excel',delimiter=',')
00026     fieldnames=['t1','t2','t3']
00027     r.writeRow(fieldnames)
00028     data=[1,2,'a']
00029     r.writeRow(data)
00030     data=[11,22,'aa']
00031     r.writeRow(data)
00032     #print csv.list_dialects()
00033     r.writeRow(data)
00034     delivered=[['run','deliveredls','delivered'],[1234,23445,123.456],[2348,20,8893.0]]
00035     r.writeRows(delivered)
00036     r.close()