def _guessType(self, val): """Guess type for new dataset.""" v, ok = self.numericlocale.toDouble(val) if ok: return 'float' m = self.datere.match(val) try: utils.dateREMatchToDate(m) return 'date' except ValueError: return 'string'
def _handleVal(self, colnum, col): """Handle a value from the file. colnum: number of column col: data value """ if colnum not in self.colnames: # ignore blanks if col.strip() == '': return # process value self._newValueInBlankColumn(colnum, col) # ignore lines after headers if self.colignore[colnum] > 0: self.colignore[colnum] -= 1 return # process value if data type unknown if self.coltypes[colnum] == 'unknown': self._newUnknownDataValue(colnum, col) ctype = self.coltypes[colnum] try: # convert text to data type of column if ctype == 'float': v, ok = self.numericlocale.toDouble(col) if not ok: raise ValueError elif ctype == 'date': m = self.datere.match(col) v = utils.dateREMatchToDate(m) elif ctype == 'string': v = col else: raise RuntimeError, "Invalid type in CSV reader" except ValueError: self._handleFailedConversion(colnum, col) else: # conversion succeeded - append number to data self.data[self.colnames[colnum]].append(v)