LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
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