CMS 3D CMS Logo

csvReporter.py
Go to the documentation of this file.
1 import csv
3  def __init__(self,filename,dialect='excel',delimiter=',',quoting=csv.QUOTE_NONNUMERIC):
4  """input params:
5  filename : output csv path/name
6  """
7  self.__filename=filename
8  self.__file=open(self.__filename,'wb')
9  self.__writer=csv.writer(self.__file)
10  def writeRow(self,row):
11  try:
12  self.__writer.writerow(row)
13  except csv.Error as e:
14  sys.exit('file %s: %s'%(self.__filename,e))
15  def writeRows(self,rows):
16  try:
17  self.__writer.writerows(rows)
18  except csv.Error as e:
19  sys.exit('file %s: %s'%(self.__filename,e))
20  def close(self):
21  self.__file.close()
22 
23 if __name__ == '__main__':
24  filename='testwrite.csv'
25  r=csvReporter(filename,dialect='excel',delimiter=',')
26  fieldnames=['t1','t2','t3']
27  r.writeRow(fieldnames)
28  data=[1,2,'a']
29  r.writeRow(data)
30  data=[11,22,'aa']
31  r.writeRow(data)
32  #print csv.list_dialects()
33  r.writeRow(data)
34  delivered=[['run','deliveredls','delivered'],[1234,23445,123.456],[2348,20,8893.0]]
35  r.writeRows(delivered)
36  r.close()
def writeRow(self, row)
Definition: csvReporter.py:10
def writeRows(self, rows)
Definition: csvReporter.py:15
def __init__(self, filename, dialect='excel', delimiter=', quoting=csv.QUOTE_NONNUMERIC)
Definition: csvReporter.py:3