How to use the csvkit.table function in csvkit

To help you get started, we’ve selected a few csvkit examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github wireservice / csvkit / tests / test_table.py View on Github external
def test_table_uneven_columns(self):
        c = table.Column(0, u'test', [u'test', u'column', u''])
        c_short = table.Column(0, u'test', [u'test'])
        c_long = table.Column(0, u'test', [u'', u'', u'', u'way out here'])
        t = table.Table([c, c_short, c_long])
        self.assertEqual(t.row(0), [u'test', u'test', None])
        self.assertEqual(t.row(1), [u'column', None, None])
        self.assertEqual(t.row(2), [None, None, None])
        self.assertEqual(t.row(3), [None, None, u'way out here'])
github wireservice / csvkit / tests / test_sql.py View on Github external
def setUp(self):
        self.csv_table = table.Table([
            table.Column(0, u'text', [u'Chicago Reader', u'Chicago Sun-Times', u'Chicago Tribune', u'Row with blanks']),
            table.Column(1, u'integer', [u'40', u'63', u'164', u'']),
            table.Column(2, u'datetime', [u'Jan 1, 2008 at 4:40 AM', u'2010-01-27T03:45:00', u'3/1/08 16:14:45', u'']),
            table.Column(3, u'empty_column', [u'', u'', u'', u''])],
            name='test_table')
github wireservice / csvkit / tests / test_table.py View on Github external
def test_table_reverse(self):
        t = table.Table()
        self.assertRaises(NotImplementedError, t.reverse)
github wireservice / csvkit / tests / test_sql.py View on Github external
def setUp(self):
        self.csv_table = table.Table([
            table.Column(0, u'text', [u'Chicago Reader', u'Chicago Sun-Times', u'Chicago Tribune', u'Row with blanks']),
            table.Column(1, u'integer', [u'40', u'63', u'164', u'']),
            table.Column(2, u'datetime', [u'Jan 1, 2008 at 4:40 AM', u'2010-01-27T03:45:00', u'3/1/08 16:14:45', u'']),
            table.Column(3, u'empty_column', [u'', u'', u'', u''])],
            name='test_table')
github wireservice / csvkit / tests / test_table.py View on Github external
def setUp(self):
        self.c = table.Column(0, u'test', [u'test', u'column', None])
        self.c2 = table.Column(0, u'test', [0, 1, 42], normal_type=int)
        self.c3 = table.Column(0, u'test', [datetime.datetime(2007, 1, 1, 12, 13, 14)], normal_type=datetime.datetime)
github mvanderkroon / cobr / api / HttpODBC.py View on Github external
def csv2sql(file=None, db_schema=None, tablename=None, encoding='utf-8', snifflimit=512*1024):

    try:
        conn = engine.connect()
        trans = conn.begin()

        csv_table = table.Table.from_csv(
            file.stream,
            name=tablename,
            snifflimit=snifflimit,
            blanks_as_nulls=True,
            infer_types=True,
            no_header_row=False,
            encoding=encoding
        )

        sql_table = sql.make_table(
            csv_table,
            tablename,
            False, #self.args.no_constraints
            db_schema, #self.args.db_schema
            metadata
        )
github mitmedialab / DataBasic / databasic / logic / wtfcsvstat.py View on Github external
summary_start = time.clock()
        results = {}
        
        operations = [op for op in OPERATIONS]

        if self._csv_has_rows(self.input_path) == False:
            results['row_count'] = 0
            results['columns'] = []
            return results

        start_time = time.clock()
        tab = None
        delim = self.detectDelimiter()

        try:
            tab = table.Table.from_csv(self.input_file,delimiter=delim,quotechar='"')
        except Exception as e:
            logger.debug("Error making a table from the CSV")
            return 'bad_formatting'

        logger.debug("  %f ms to create table from csv" % (1000*(time.clock()-start_time)))

        row_count = tab.count_rows() + 1 # this value is inaccurate so I'm adding 1
        if self.has_header_row:
            row_count -= 1
        results['row_count'] = row_count
        logger.debug("  found %d rows" % row_count)

        column_count = len(tab)
        empty_header_count = 0
        
        results['columns'] = []
github mitmedialab / DataBasic / databasic / logic / connectthedots.py View on Github external
def __init__(self, input_path, has_header_row=True):
        """
        Initialize the object
        """
        utf8_file_path = filehandler.convert_to_utf8(input_path)
        input_file = codecs.open(utf8_file_path, 'r', filehandler.ENCODING_UTF_8)
        try:
            self.table = table.Table.from_csv(input_file,
                                              no_header_row=not has_header_row,
                                              snifflimit=0,
                                              blanks_as_nulls=False)
            if (len(self.table) != 2):
                raise ValueError('File has more than two columns')
            else:
                self.graph = nx.from_edgelist(self.table.to_rows())
        except Exception as e:
            logger.error('[CTD] Unable to make table from csv: %s' % e)
github fishtown-analytics / dbt / dbt / seeder.py View on Github external
def do_seed(self, schema, cursor, drop_existing):
        existing_tables = self.existing_tables(cursor, schema)

        csvs = self.find_csvs()
        statuses = []
        for csv in csvs:

            table_name = csv.name
            fh = open(csv.filepath)
            virtual_table = csv_table.Table.from_csv(fh, table_name)

            if table_name in existing_tables:
                if drop_existing:
                    self.drop_table(cursor, schema, table_name)
                    self.create_table(
                        cursor,
                        schema,
                        table_name,
                        virtual_table
                    )
                else:
                    self.truncate_table(cursor, schema, table_name)
            else:
                self.create_table(cursor, schema, table_name, virtual_table)

            try:
github GeoNode / geonode / geonode / contrib / datatables / utils.py View on Github external
table_name = data_table.table_name
    else:
        # Get a unique name for the data table
        table_name = os.path.splitext(os.path.basename(csv_filename))[0]
        table_name = get_unique_tablename(table_name)

        data_table.table_name = table_name
        data_table.save()

    # -----------------------------------------------------
    # Transform csv file to csvkit Table
    # -----------------------------------------------------
    csv_file_handle = open(csv_filename, 'rb')

    try:
        csv_table = table.Table.from_csv(\
                                csv_file_handle,
                                name=table_name,
                                no_header_row=no_header_row,
                                delimiter=delimiter)
    except:
        data_table.delete()
        err_msg = str(sys.exc_info()[0])
        LOGGER.error('Failed to convert csv file to table.  Error: %s'\
                , err_msg)
        return None, err_msg
    #csv_file = File(f)
    csv_file_handle.close()

    # -----------------------------------------------------
    # If needed, force a column to be character
    # -----------------------------------------------------