How to use the agate.csv.reader function in agate

To help you get started, we’ve selected a few agate 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_utilities / test_csvstat.py View on Github external
def test_csv(self):
        output = self.get_output_as_io(['--csv', 'examples/realdata/ks_1033_data.csv'])

        reader = agate.csv.reader(output)

        header = next(reader)

        self.assertEqual(header[1], 'column_name')
        self.assertEqual(header[4], 'unique')

        row = next(reader)

        self.assertEqual(row[1], 'state')
        self.assertEqual(row[2], 'Text')
        self.assertEqual(row[5], '')
        self.assertEqual(row[11], '2')
github wireservice / csvkit / tests / utils.py View on Github external
def get_output_as_reader(self, args):
        return agate.csv.reader(self.get_output_as_io(args))
github wireservice / csvkit / csvkit / cli.py View on Github external
def print_column_names(self):
        """
        Pretty-prints the names and indices of all columns to a file-like object (usually sys.stdout).
        """
        if getattr(self.args, 'no_header_row', None):
            raise RequiredHeaderError('You cannot use --no-header-row with the -n or --names options.')

        if getattr(self.args, 'zero_based', None):
            start = 0
        else:
            start = 1

        rows = agate.csv.reader(self.skip_lines(), **self.reader_kwargs)
        column_names = next(rows)

        for i, c in enumerate(column_names, start):
            self.output_file.write('%3i: %s\n' % (i, c))
github wireservice / agate / agate / table / from_csv.py View on Github external
else:
        raise ValueError('skip_lines argument must be an int')

    start = f.tell()

    if sniff_limit is None:
        kwargs['dialect'] = csv.Sniffer().sniff(f.read())
    elif sniff_limit > 0:
        kwargs['dialect'] = csv.Sniffer().sniff(f.read(sniff_limit))

    f.seek(start)

    if six.PY2:
        f = six.StringIO(f.read().encode('utf-8'))

    reader = csv.reader(f, header=header, **kwargs)

    if header:
        if column_names is None:
            column_names = next(reader)
        else:
            next(reader)

    rows = tuple(reader)

    if close:
        f.close()

    return Table(rows, column_names, column_types, row_names=row_names)
github wireservice / csvkit / csvkit / utilities / csvclean.py View on Github external
def main(self):
        if self.additional_input_expected():
            sys.stderr.write('No input file or piped data provided. Waiting for standard input:\n')

        reader = agate.csv.reader(self.skip_lines(), **self.reader_kwargs)

        if self.args.dryrun:
            checker = RowChecker(reader)

            for row in checker.checked_rows():
                pass

            if checker.errors:
                for e in checker.errors:
                    self.output_file.write('Line %i: %s\n' % (e.line_number, e.msg))
            else:
                self.output_file.write('No errors.\n')

            if checker.joins:
                self.output_file.write('%i rows would have been joined/reduced to %i rows after eliminating expected internal line breaks.\n' % (checker.rows_joined, checker.joins))
        else:
github wireservice / csvkit / csvkit / utilities / csvjson.py View on Github external
def streaming_output_ndjson(self):
        rows = agate.csv.reader(self.input_file, **self.reader_kwargs)
        column_names = next(rows)

        for row in rows:
            data = OrderedDict()
            for i, column in enumerate(column_names):
                try:
                    data[column] = row[i]
                except IndexError:
                    data[column] = None
            self.dump_json(data, newline=True)
github wireservice / csvkit / csvkit / utilities / csvformat.py View on Github external
def main(self):
        if self.additional_input_expected():
            sys.stderr.write('No input file or piped data provided. Waiting for standard input:\n')

        reader = agate.csv.reader(self.skip_lines(), **self.reader_kwargs)
        writer = agate.csv.writer(self.output_file, **self.writer_kwargs)
        writer.writerows(reader)
github wireservice / csvkit / csvkit / utilities / csvstat.py View on Github external
operations = [op for op in OPERATIONS.keys() if getattr(self.args, op + '_only')]

        if len(operations) > 1:
            self.argparser.error('Only one operation argument may be specified (--mean, --median, etc).')

        if operations and self.args.csv_output:
            self.argparser.error('You may not specify --csv and an operation (--mean, --median, etc) at the same time.')

        if operations and self.args.count_only:
            self.argparser.error('You may not specify --count and an operation (--mean, --median, etc) at the same time.')

        if six.PY2:
            self.output_file = codecs.getwriter('utf-8')(self.output_file)

        if self.args.count_only:
            count = len(list(agate.csv.reader(self.skip_lines(), **self.reader_kwargs)))

            if not self.args.no_header_row:
                count -= 1

            self.output_file.write('Row count: %i\n' % count)

            return

        table = agate.Table.from_csv(
            self.input_file,
            skip_lines=self.args.skip_lines,
            sniff_limit=self.args.sniff_limit,
            **self.reader_kwargs
        )

        column_ids = parse_column_identifiers(
github wireservice / csvkit / csvkit / utilities / csvjson.py View on Github external
def streaming_output_ndgeojson(self):
        rows = agate.csv.reader(self.input_file, **self.reader_kwargs)
        column_names = next(rows)
        geojson_generator = self.GeoJsonGenerator(self.args, column_names)

        for row in rows:
            self.dump_json(geojson_generator.feature_for_row(row), newline=True)
github wireservice / csvkit / csvkit / __init__.py View on Github external
"""
This module contains csvkit's superpowered alternative to the standard Python
CSV reader and writer. It can be used as a drop-in replacement for the standard
module.

.. warn::

    Since version 1.0 csvkit relies on `agate `_'s
CSV reader and writer. This module is supported for legacy purposes only and you
should migrate to using agate.
"""

import agate

reader = agate.csv.reader
writer = agate.csv.writer
DictReader = agate.csv.DictReader
DictWriter = agate.csv.DictWriter