How to use the petl.io.sources.write_source_from_arg function in petl

To help you get started, we’ve selected a few petl 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 petl-developers / petl / petl / io / pickle.py View on Github external
def _writepickle(table, source, mode, protocol, write_header):
    source = write_source_from_arg(source)
    with source.open(mode) as f:
        it = iter(table)
        hdr = next(it)
        if write_header:
            pickle.dump(hdr, f, protocol)
        for row in it:
            pickle.dump(row, f, protocol)
github petl-developers / petl / src / petl / io / csv.py View on Github external
def appenducsv(table, source=None, dialect='excel', encoding='utf-8',
               write_header=False, **kwargs):
    """Append the table to a delimited file using the given text encoding. Like
    :func:`appendcsv` but accepts an additional ``encoding`` argument which
    should be one of the Python supported encodings.

    """

    source = write_source_from_arg(source)
    appenducsv_impl(table, source=source, dialect=dialect, encoding=encoding,
                    write_header=write_header, **kwargs)
github petl-developers / petl / src / petl / io / text.py View on Github external
def appendtext(table, source=None, template=None, prologue=None, epilogue=None):
    """Append the table to a text file.

    """

    assert template is not None, 'template is required'
    source = write_source_from_arg(source)
    with source.open_('ab') as f:
        if PY2:
            # write direct to buffer
            _writetext(table, f, prologue, template, epilogue)
        else:
            # wrap buffer for text encoding
            f = io.TextIOWrapper(f, encoding='ascii', newline='',
                                 write_through=True)
            try:
                _writetext(table, f, prologue, template, epilogue)
            finally:
                f.detach()
github petl-developers / petl / src / petl / io / csv.py View on Github external
def toucsv(table, source=None, dialect='excel', encoding='utf-8',
           write_header=True, **kwargs):
    """Write the table to a delimited file using the given text encoding. Like
    :func:`tocsv` but accepts an additional ``encoding`` argument which
    should be one of the Python supported encodings.

    """

    source = write_source_from_arg(source)
    toucsv_impl(table, source=source, dialect=dialect, encoding=encoding,
                write_header=write_header, **kwargs)
github petl-developers / petl / src / petl / io / text.py View on Github external
def appendutext(table, source=None, encoding='utf-8', template=None,
                prologue=None, epilogue=None):
    """Append the table to a text file via the given encoding. Like
    :func:`appendtext` but accepts an additional ``encoding`` argument which
    should be one of the Python supported encodings.

    """

    assert template is not None, 'template is required'
    source = write_source_from_arg(source)
    with source.open_('ab') as f:
        if PY2:
            f = codecs.getwriter(encoding)(f)
            _writetext(table, f, prologue, template, epilogue)
        else:
            f = io.TextIOWrapper(f, encoding=encoding, newline='',
                                 write_through=True)
            try:
                _writetext(table, f, prologue, template, epilogue)
            finally:
                f.detach()
github petl-developers / petl / src / petl / io / csv.py View on Github external
def teeucsv(table, source=None, write_header=True, dialect='excel',
            encoding='utf-8', **kwargs):
    """Returns a table that writes rows to a Unicode CSV file as they are
    iterated over.

    """

    source = write_source_from_arg(source)
    return teeucsv_impl(table, source=source, encoding=encoding,
                        write_header=write_header, dialect=dialect, **kwargs)
github petl-developers / petl / src / petl / io / pickle.py View on Github external
def _writepickle(table, source, mode, protocol, write_header):
    source = write_source_from_arg(source)
    with source.open_(mode) as f:
        it = iter(table)
        hdr = next(it)
        if write_header:
            pickle.dump(hdr, f, protocol)
        for row in it:
            pickle.dump(row, f, protocol)
github petl-developers / petl / src / petl / io / json.py View on Github external
def _writejson(source, obj, prefix, suffix, *args, **kwargs):
    encoder = JSONEncoder(*args, **kwargs)
    source = write_source_from_arg(source)
    with source.open_('wb') as f:
        if PY2:
            # write directly to buffer
            _writeobj(encoder, obj, f, prefix, suffix)
        else:
            # wrap buffer for text IO
            f = io.TextIOWrapper(f, encoding='utf-8', newline='',
                                 write_through=True)
            try:
                _writeobj(encoder, obj, f, prefix, suffix)
            finally:
                f.detach()
github petl-developers / petl / src / petl / io / html.py View on Github external
def touhtml(table, source=None, caption=None, encoding='utf-8',
            representation=text_type, lineterminator='\r\n'):
    """Write the table as HTML to a text file using the given encoding.

    """

    source = write_source_from_arg(source)
    with source.open_('wb') as f:
        if PY2:
            f = codecs.getwriter(encoding)(f)
        else:
            f = io.TextIOWrapper(f, encoding=encoding, newline='')
        try:
            it = iter(table)
            flds = next(it)
            _write_begin(f, flds, lineterminator, caption)
            for row in it:
                _write_row(f, row, lineterminator, representation)
            _write_end(f, lineterminator)
        finally:
            if not PY2:
                f.detach()
github petl-developers / petl / src / petl / io / text.py View on Github external
def __iter__(self):
        source = write_source_from_arg(self.source)
        with source.open_('wb') as f:
            if PY2:
                # write direct to buffer
                for row in _teetext(self.table, f, self.prologue, self.template,
                                    self.epilogue):
                    yield row
            else:
                # wrap buffer for text encoding
                f = io.TextIOWrapper(f, encoding='ascii', newline='',
                                     write_through=True)
                try:
                    for row in _teetext(self.table, f, self.prologue,
                                        self.template, self.epilogue):
                        yield row
                finally:
                    f.detach()