def convert(f, format, schema=None, key=None, **kwargs): """ Convert a file of a specified format to CSV. """ if not f: raise ValueError('f must not be None') if not format: raise ValueError('format must not be None') if format == 'fixed': if not schema: raise ValueError('schema must not be null when format is "fixed"') return fixed2csv(f, schema, **kwargs) elif format == 'xls': return xls2csv(f, **kwargs) elif format == 'xlsx': return xlsx2csv(f, **kwargs) elif format == 'json': return json2csv(f, key, **kwargs) elif format == 'ndjson': return ndjson2csv(f, **kwargs) elif format == 'geojson': return geojson2csv(f, **kwargs) elif format == 'csv': return csv2csv(f, **kwargs) elif format == 'dbf': if six.PY3: raise ValueError('format "dbf" is not supported forthis version of Python.') return dbf2csv(f, **kwargs) else: raise ValueError('format "%s" is not supported' % format)
def convert(f, format, schema=None, key=None, **kwargs): """ Convert a file of a specified format to CSV. """ if not f: raise ValueError('f must not be None') if not format: raise ValueError('format must not be None') if format == 'fixed': if not schema: raise ValueError('schema must not be null when format is "fixed"') return fixed2csv(f, schema, **kwargs) elif format == 'xls': return xls2csv(f, **kwargs) elif format == 'xlsx': return xlsx2csv(f, **kwargs) elif format == 'json': return json2csv(f, key, **kwargs) elif format == 'ndjson': return ndjson2csv(f, **kwargs) elif format == 'geojson': return geojson2csv(f, **kwargs) elif format == 'csv': return csv2csv(f, **kwargs) elif format == 'dbf': if six.PY3: raise ValueError( 'format "dbf" is not supported forthis version of Python.') return dbf2csv(f, **kwargs) else: raise ValueError('format "%s" is not supported' % format)
def getReader(uploadfile): file = uploadfile.file extension = os.path.splitext(uploadfile.filename)[1] csvreader = None # make sure to convert excel files if extension == '.xls': file = StringIO.StringIO(xls2csv(file)) csvreader = reader(file) else: dialect = sniffer.sniff_dialect(file.read(4096)) file.seek(0) csvreader = reader(file, dialect=dialect) return csvreader
def test_xls(self): with open('examples/test.xls', 'r') as f: output = xls.xls2csv(f) with open('examples/testxls_converted.csv', 'r') as f: self.assertEquals(f.read(), output)
def test_xls_with_sheet(self): with open('examples/sheets.xls', 'rb') as f: output = xls.xls2csv(f, sheet='data') with open('examples/testxls_converted.csv', 'r') as f: self.assertEquals(f.read(), output)
def test_xls_with_sheet(self): with open('examples/sheets.xls', 'rb') as f: output = xls.xls2csv(f, sheet='Sheet2') with open('examples/sheetsxls_converted.csv', 'r') as f: self.assertEquals(f.read(), output)
def test_xls(self): with open('examples/test.xls', 'rb') as f: output = xls.xls2csv(f) with open('examples/testxls_converted.csv', 'r') as f: self.assertEquals(f.read(), output)