[Python-checkins] python/nondist/sandbox/csv README,1.1,1.2 csv.py,1.1,1.2
andrewmcnamara@users.sourceforge.net
andrewmcnamara@users.sourceforge.net
Wed, 29 Jan 2003 06:18:22 -0800
Update of /cvsroot/python/python/nondist/sandbox/csv
In directory sc8-pr-cvs1:/tmp/cvs-serv32266
Modified Files:
README csv.py
Log Message:
Simplistic python module to implement some of the proposed PEP on top of the
existing C _csv module.
Index: README
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/csv/README,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** README 28 Jan 2003 02:07:46 -0000 1.1
--- README 29 Jan 2003 14:18:20 -0000 1.2
***************
*** 1,5 ****
This little corner of the sandbox aims to create a module to read and write
CSV files. Involved parties are currently Kevin Altis, Dave Cole, Skip
! Montanaro and Cliff Wells.
Stay tuned...
--- 1,5 ----
This little corner of the sandbox aims to create a module to read and write
CSV files. Involved parties are currently Kevin Altis, Dave Cole, Skip
! Montanaro, Cliff Wells and Andrew McNamara.
Stay tuned...
Index: csv.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/csv/csv.py,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** csv.py 28 Jan 2003 02:07:46 -0000 1.1
--- csv.py 29 Jan 2003 14:18:20 -0000 1.2
***************
*** 1 ****
! # placeholder
--- 1,71 ----
! import _csv
!
! class excel2000:
! pass
!
! class excel2000_tab:
! field_sep='\t'
!
! dialects = {
! 'excel': excel2000,
! 'excel2000': excel2000,
! 'excel-tab': excel2000_tab,
! 'excel2000-tab': excel2000_tab,
! }
!
! class Error(Exception):
! pass
!
! class OCcvs:
! def __init__(self, dialect, **options):
! try:
! dialect_obj = dialects[dialect]
! except KeyError:
! raise Error('Unknown dialect')
! parser_options = {}
! for attr in dir(dialect_obj):
! if attr[0] == '_':
! continue
! parser_options[attr] = getattr(dialect_obj, attr)
! parser_options.update(options)
! try:
! self.parser = _csv.parser(**parser_options)
! except _csv.Error, e:
! raise Error(e)
!
! class reader(OCcvs):
! def __init__(self, fileobj, dialect = 'excel2000', **options):
! self.fileobj = fileobj
! OCcvs.__init__(self, dialect, **options)
!
! def __iter__(self):
! return self
!
! def next(self):
! while 1:
! fields = self.parser.parse(self.fileobj.next())
! if fields:
! return fields
!
! class writer(OCcvs):
! def __init__(self, fileobj, dialect='excel2000', **options):
! self.fileobj = fileobj
! OCcvs.__init__(self, dialect, **options)
!
! def write(self, fields):
! self.fileobj.write(self.parser.join(fields) + '\n')
!
! def writelines(self, lines):
! for fields in lines:
! self.write(fields)
!
! def close(self):
! self.fileobj.close()
! del self.fileobj
!
! def __del__(self):
! if hasattr(self, 'fileobj'):
! try:
! self.close()
! except:
! pass