From 7c852f33a2b095503c9b745a0798d5c9deee6982 Mon Sep 17 00:00:00 2001 From: Fred Drake Date: Fri, 25 Apr 2003 14:27:00 +0000 Subject: [PATCH] Attempt to deal with some obvious errors in the code. These were all due to using a single module-level namespace where multiple namespaces were used before. There *really* need to be tests for the sniffer stuff. This could have been avoided. Skip, please review, and add sniffer tests! --- Lib/csv.py | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/Lib/csv.py b/Lib/csv.py index c510c704b6c..89d86d6ce02 100644 --- a/Lib/csv.py +++ b/Lib/csv.py @@ -161,7 +161,6 @@ class Sniffer: """ Takes a file-like object and returns a dialect (or None) """ - self.fileobj = fileobj data = fileobj.read(self.sample) @@ -171,17 +170,17 @@ class Sniffer: if delimiter is None: delimiter, skipinitialspace = self._guessDelimiter(data) - class Dialect(csv.Dialect): + class SniffedDialect(Dialect): _name = "sniffed" lineterminator = '\r\n' - quoting = csv.QUOTE_MINIMAL + quoting = QUOTE_MINIMAL # escapechar = '' doublequote = False - Dialect.delimiter = delimiter - Dialect.quotechar = quotechar - Dialect.skipinitialspace = skipinitialspace + SniffedDialect.delimiter = delimiter + SniffedDialect.quotechar = quotechar + SniffedDialect.skipinitialspace = skipinitialspace - self.dialect = Dialect + self.dialect = SniffedDialect return self.dialect @@ -189,8 +188,8 @@ class Sniffer: return self._hasHeaders(self.fileobj, self.dialect) - def register_dialect(self, name = 'sniffed'): - csv.register_dialect(name, self.dialect) + def register_dialect(self, name='sniffed'): + register_dialect(name, self.dialect) def _guessQuoteAndDelimiter(self, data): @@ -378,19 +377,19 @@ class Sniffer: # objects... fileobj.seek(0) - reader = csv.reader(fileobj, - delimiter = dialect.delimiter, - quotechar = dialect.quotechar, - skipinitialspace = dialect.skipinitialspace) + r = csv.reader(fileobj, + delimiter=dialect.delimiter, + quotechar=dialect.quotechar, + skipinitialspace=dialect.skipinitialspace) - header = reader.next() # assume first row is header + header = r.next() # assume first row is header columns = len(header) columnTypes = {} for i in range(columns): columnTypes[i] = None checked = 0 - for row in reader: + for row in r: # arbitrary number of rows to check, to keep it sane if checked > 20: break