LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
LSSTDataManagementBasePackage
csvFileWriter.py
Go to the documentation of this file.
1 import gzip
2 import re
3 import lsst.daf.base as dafBase
4 
5 class CsvFileWriter(object):
6  def __init__(self, path, overwrite=True, compress=True):
7  if compress:
8  self.f = gzip.open(path + ".gz", "w" if overwrite else "a")
9  else:
10  self.f = open(path, "w" if overwrite else "a")
11 
12  def __del__(self):
13  self.f.close()
14 
15  def flush(self):
16  self.f.flush()
17 
18  def quote(self, value):
19  if value is None:
20  return '\N'
21  if isinstance(value, float):
22  return "%.17g" % (value,)
23  if isinstance(value, str):
24  value = re.sub(r'"', r'\"', value)
25  return '"' + value.strip() + '"'
26  if isinstance(value, dafBase.DateTime):
27  value = value.toString()
28  return '"' + value[0:10] + ' ' + value[11:19] + '"'
29  return str(value)
30 
31  def write(self, *fields):
32  print >>self.f, ",".join([self.quote(field) for field in fields])
Class for handling dates/times, including MJD, UTC, and TAI.
Definition: DateTime.h:58