Esempio n. 1
0
    def to_csv(self, path, **kwargs):
        """
        Write this table to a CSV. This method uses agate's builtin CSV writer,
        which supports unicode on both Python 2 and Python 3.

        ``kwargs`` will be passed through to the CSV writer.

        :param path: Filepath or file-like object to write to.
        """
        if 'lineterminator' not in kwargs:
            kwargs['lineterminator'] = '\n'

        close = True

        try:
            if hasattr(path, 'write'):
                f = path
                close = False
            else:
                f = open(path, 'w')

            writer = csv.writer(f, **kwargs)

            writer.writerow(self._column_names)

            for row in self._rows:
                writer.writerow(row)
        finally:
            if close:
                f.close()
Esempio n. 2
0
    def test_writer_alias(self):
        output = six.StringIO()
        writer = csv_py2.writer(output, encoding='utf-8')
        self.assertEqual(writer._eight_bit, True)
        writer.writerow(['a', 'b', 'c'])
        writer.writerow(['1', '2', '3'])
        writer.writerow(['4', '5', u'ʤ'])

        written = six.StringIO(output.getvalue())

        reader = csv_py2.reader(written, encoding='utf-8')
        self.assertEqual(next(reader), ['a', 'b', 'c'])
        self.assertEqual(next(reader), ['1', '2', '3'])
        self.assertEqual(next(reader), ['4', '5', u'ʤ'])
Esempio n. 3
0
    def test_writer_alias(self):
        output = six.StringIO()
        writer = csv_py2.writer(output, encoding='utf-8')
        self.assertEqual(writer._eight_bit, True)
        writer.writerow(['a', 'b', 'c'])
        writer.writerow(['1', '2', '3'])
        writer.writerow(['4', '5', u'ʤ'])

        written = six.StringIO(output.getvalue())

        reader = csv_py2.reader(written, encoding='utf-8')
        self.assertEqual(next(reader), ['a', 'b', 'c'])
        self.assertEqual(next(reader), ['1', '2', '3'])
        self.assertEqual(next(reader), ['4', '5', u'ʤ'])
Esempio n. 4
0
    def test_writer_alias(self):
        output = six.StringIO()
        writer = csv_py2.writer(output, encoding="utf-8")
        self.assertEqual(writer._eight_bit, True)
        writer.writerow(["a", "b", "c"])
        writer.writerow(["1", "2", "3"])
        writer.writerow(["4", "5", u"ʤ"])

        written = six.StringIO(output.getvalue())

        reader = csv_py2.reader(written, encoding="utf-8")
        self.assertEqual(next(reader), ["a", "b", "c"])
        self.assertEqual(next(reader), ["1", "2", "3"])
        self.assertEqual(next(reader), ["4", "5", u"ʤ"])
Esempio n. 5
0
    def to_csv(self, path, **kwargs):
        """
        Write this table to a CSV. This method uses agate's builtin CSV writer,
        which supports unicode on both Python 2 and Python 3.

        `kwargs` will be passed through to the CSV writer.

        :param path:
            Filepath or file-like object to write to.
        """
        if 'lineterminator' not in kwargs:
            kwargs['lineterminator'] = '\n'

        close = True
        f = None

        try:
            if hasattr(path, 'write'):
                f = path
                close = False
            else:
                dirpath = os.path.dirname(path)

                if dirpath and not os.path.exists(dirpath):
                    os.makedirs(dirpath)

                f = open(path, 'w')

            writer = csv.writer(f, **kwargs)
            writer.writerow(self._column_names)

            csv_funcs = [c.csvify for c in self._column_types]

            for row in self._rows:
                writer.writerow(
                    tuple(csv_funcs[i](d) for i, d in enumerate(row)))
        finally:
            if close and f is not None:
                f.close()
Esempio n. 6
0
    def to_csv(self, path, **kwargs):
        """
        Write this table to a CSV. This method uses agate's builtin CSV writer,
        which supports unicode on both Python 2 and Python 3.

        `kwargs` will be passed through to the CSV writer.

        :param path:
            Filepath or file-like object to write to.
        """
        if "lineterminator" not in kwargs:
            kwargs["lineterminator"] = "\n"

        close = True
        f = None

        try:
            if hasattr(path, "write"):
                f = path
                close = False
            else:
                dirpath = os.path.dirname(path)

                if dirpath and not os.path.exists(dirpath):
                    os.makedirs(dirpath)

                f = open(path, "w")

            writer = csv.writer(f, **kwargs)
            writer.writerow(self._column_names)

            csv_funcs = [c.csvify for c in self._column_types]

            for row in self._rows:
                writer.writerow(tuple(csv_funcs[i](d) for i, d in enumerate(row)))
        finally:
            if close and f is not None:
                f.close()