Esempio n. 1
0
    def __iter__(self):
        with self.source.open("wb") as buf:

            # determine encoding
            codec = getcodec(self.encoding)

            # ascii
            if codec.name == "ascii":
                # bypass encoding
                writer = csv.writer(buf, **self.csvargs)

            # non-ascii
            else:
                writer = UnicodeWriter(buf, encoding=self.encoding, errors=self.errors, **self.csvargs)

            it = iter(self.table)

            # deal with header row
            hdr = next(it)
            if self.write_header:
                writer.writerow(hdr)
            # N.B., always yield header, even if we don't write it
            yield tuple(hdr)

            # data rows
            for row in it:
                writer.writerow(row)
                yield tuple(row)
Esempio n. 2
0
File: text.py Progetto: DeanWay/petl
    def __iter__(self):
        with self.source.open('rb') as buf:

            # deal with text encoding
            if PY2:
                codec = getcodec(self.encoding)
                f = codec.streamreader(buf, errors=self.errors)
            else:
                f = io.TextIOWrapper(buf,
                                     encoding=self.encoding,
                                     errors=self.errors,
                                     newline='')

            # generate the table
            try:
                if self.header is not None:
                    yield tuple(self.header)
                if self.strip is False:
                    for line in f:
                        yield (line,)
                else:
                    for line in f:
                        yield (line.strip(self.strip),)
            finally:
                if not PY2:
                    f.detach()
Esempio n. 3
0
 def __init__(self, buf, encoding=None, errors=None, **csvargs):
     # Redirect output to a queue
     self.queue = cStringIO.StringIO()
     self.writer = csv.writer(self.queue, **csvargs)
     self.stream = buf
     codec = getcodec(encoding)
     self.encoder = codec.incrementalencoder(errors)
Esempio n. 4
0
    def __iter__(self):
        table = self.table
        source = self.source
        encoding = self.encoding
        errors = self.errors
        lineterminator = self.lineterminator
        caption = self.caption
        index_header = self.index_header
        tr_style = self.tr_style
        td_styles = self.td_styles
        vrepr = self.vrepr
        truncate = self.truncate

        with source.open('wb') as buf:

            # deal with text encoding
            if PY2:
                codec = getcodec(encoding)
                f = codec.streamwriter(buf, errors=errors)
            else:
                f = io.TextIOWrapper(buf,
                                     encoding=encoding,
                                     errors=errors,
                                     newline='')

            # write the table
            try:
                it = iter(table)

                # write header
                hdr = next(it)
                _write_begin(f, hdr, lineterminator, caption, index_header,
                             truncate)
                yield hdr

                # write body
                if tr_style and callable(tr_style):
                    # wrap as records
                    it = (Record(row, hdr) for row in it)
                for row in it:
                    _write_row(f, hdr, row, lineterminator, vrepr,
                               tr_style, td_styles, truncate)
                    yield row

                # finish up
                _write_end(f, lineterminator)
                f.flush()

            finally:
                if not PY2:
                    f.detach()
Esempio n. 5
0
def _writecsv(table, source, mode, write_header, encoding, errors, **csvargs):
    rows = table if write_header else data(table)
    with source.open(mode) as buf:

        # determine encoding
        codec = getcodec(encoding)

        # ascii
        if codec.name == "ascii":
            # bypass encoding
            writer = csv.writer(buf, **csvargs)

        # non-ascii
        else:
            writer = UnicodeWriter(buf, encoding=encoding, errors=errors, **csvargs)

        for row in rows:
            writer.writerow(row)
Esempio n. 6
0
    def __iter__(self):

        # determine encoding
        codec = getcodec(self.encoding)

        # ascii
        if codec.name == "ascii":
            # bypass encoding
            with self.source.open("rU") as csvfile:
                reader = csv.reader(csvfile, **self.csvargs)
                for row in reader:
                    yield tuple(row)

        # non-ascii
        else:
            with self.source.open("rb") as buf:
                reader = UnicodeReader(buf, encoding=self.encoding, errors=self.errors, **self.csvargs)
                for row in reader:
                    yield tuple(row)
Esempio n. 7
0
File: text.py Progetto: DeanWay/petl
def _iterteetext(table, source, encoding, errors, template, prologue, epilogue):

    # guard conditions
    assert template is not None, 'template is required'

    # prepare source
    source = write_source_from_arg(source)

    with source.open('wb') as buf:

        # deal with text encoding
        if PY2:
            codec = getcodec(encoding)
            f = codec.streamwriter(buf, errors=errors)
        else:
            f = io.TextIOWrapper(buf,
                                 encoding=encoding,
                                 errors=errors)

        # write the data
        try:
            if prologue is not None:
                f.write(prologue)
            it = iter(table)
            hdr = next(it)
            yield tuple(hdr)
            flds = list(map(text_type, hdr))
            for row in it:
                rec = asdict(flds, row)
                s = template.format(**rec)
                f.write(s)
                yield row
            if epilogue is not None:
                f.write(epilogue)
            f.flush()

        finally:
            if not PY2:
                f.detach()
Esempio n. 8
0
    def __iter__(self):
        if self.header is not None:
          yield tuple(self.header)

        # determine encoding
        codec = getcodec(self.encoding)

        # ascii
        if codec.name == 'ascii':
            # bypass encoding
            with self.source.open('rU') as csvfile:
                reader = csv.reader(csvfile, **self.csvargs)
                for row in reader:
                    yield tuple(row)

        # non-ascii
        else:
            with self.source.open('rb') as buf:
                reader = UnicodeReader(buf, encoding=self.encoding,
                                       errors=self.errors, **self.csvargs)
                for row in reader:
                    yield tuple(row)
Esempio n. 9
0
 def __init__(self, buf, encoding, errors):
     codec = getcodec(encoding)
     self.reader = codec.streamreader(buf, errors=errors)
Esempio n. 10
0
 def __init__(self, buf, encoding, errors):
     codec = getcodec(encoding)
     self.reader = codec.streamreader(buf, errors=errors)
Esempio n. 11
0
def tohtml(table, source=None, encoding=None, errors=None, caption=None,
           vrepr=text_type, lineterminator='\n', index_header=False,
           tr_style=None, td_styles=None, truncate=None):
    """
    Write the table as HTML to a file. E.g.::

        >>> import petl as etl
        >>> table1 = [['foo', 'bar'],
        ...           ['a', 1],
        ...           ['b', 2],
        ...           ['c', 2]]
        >>> etl.tohtml(table1, 'example.html', caption='example table')
        >>> print(open('example.html').read())
        <table class='petl'>
        <caption>example table</caption>
        <thead>
        <tr>
        <th>foo</th>
        <th>bar</th>
        </tr>
        </thead>
        <tbody>
        <tr>
        <td>a</td>
        <td style='text-align: right'>1</td>
        </tr>
        <tr>
        <td>b</td>
        <td style='text-align: right'>2</td>
        </tr>
        <tr>
        <td>c</td>
        <td style='text-align: right'>2</td>
        </tr>
        </tbody>
        </table>

    The `caption` keyword argument is used to provide a table caption
    in the output HTML.

    """

    source = write_source_from_arg(source)
    with source.open('wb') as buf:

        # deal with text encoding
        if PY2:
            codec = getcodec(encoding)
            f = codec.streamwriter(buf, errors=errors)
        else:
            f = io.TextIOWrapper(buf,
                                 encoding=encoding,
                                 errors=errors,
                                 newline='')

        # write the table
        try:
            it = iter(table)

            # write header
            hdr = next(it)
            _write_begin(f, hdr, lineterminator, caption, index_header,
                         truncate)

            # write body
            if tr_style and callable(tr_style):
                # wrap as records
                it = (Record(row, hdr) for row in it)
            for row in it:
                _write_row(f, hdr, row, lineterminator, vrepr,
                           tr_style, td_styles, truncate)

            # finish up
            _write_end(f, lineterminator)
            f.flush()

        finally:
            if not PY2:
                f.detach()