Skip to main content

A tool to generate csv file from a list of python instances

Project description

Introduction

Any2csv is a helper module to ease producing CSV files from different sources. It is mainly usefull to consume SQLAlchemy requests results or any other ORM that produces objects.

Licence

This package is covered by the permissive BSD licence.

Python versions

Any2csv is tested with the help of tox on python 2.7 and python 3.4 It may work with older versions (some parts were developped using python 2.3) but no tests or even garantee is made on this.

Example Usage

from any2csv import Any2CSV


class SubObj(object):
        def __init__(self, v):
                self.field1 = v
                self.field2 = "%s%s" % ("field2", v)


class MyObj(object):
        def __init__(self, v):
                self.field1 = v
                self.field2 = "%s%s" % ("field2", v)
                self.subobj = SubObj(v)

vals = ['a', 'b', 'c']
objlist = [MyObj(val) for val in vals]

writer = Any2CSV(
        target_filename='testoutput1.csv',
        column_mappings=[
                {'attr': 'field1', 'colname': 'field 1'},
                {'attr': 'field2', 'colname': 'field 2'},
                {'attr': 'subobj.field1', 'colname': 'field 1 of subobj'},
                {'attr': 'subobj.field2', 'colname': 'field 2 of subobj'},
        ]
)

writer.write(objlist)

In this example we use a simple in-memory list with dummy objects but the Any2CSV adapter works with any iterator as long as it outputs objects and you are able to describle the source attr and the desired colname.

If you want to apply rendering functions to your column you need to add a renderer definition to the column_mappings, here is how to do it:

import logging
from any2csv import Any2CSV


class SubObj(object):
        def __init__(self, v):
                self.field1 = v
                self.field2 = "%s_%s" % ("field2", v)


class MyObj(object):
        def __init__(self, v):
                self.field1 = v
                self.field2 = "%s_%s" % ("field2", v)
                self.subobj = SubObj(v)


def render_split(value=None):
        if value is None:
                v = u''
        else:
                v = value

        return v.split('_')[1]

logging.basicConfig()
vals = ['a', 'b', 'c']
objlist = [MyObj(val) for val in vals]

writer = Any2CSV(
        'testoutput2.csv',
        [
                {'attr': 'field1', 'colname': 'field 1'},
                {'attr': 'field2', 'colname': 'field 2', 'renderer': render_split},
                {'attr': 'subobj.field1', 'colname': 'field 1 of subobj'},
                {'attr': 'subobj.field2', 'colname': 'field 2 of subobj'},
        ],
        show_first_line=True,
)

writer.write(objlist)

Changelog

0.3.4 Nov. 5 2015

  • Fixed unicode support in Python2 (and reworked it in Python3)

  • Added possibility to pass iterables of dictionaries (as opposed to only instances) to the writer. For the moment the passed dict must have keys that match to target output columns, but we may add support for columns remapping in the future.

0.3.3 Jul. 29 2015

  • Now based on any2 for the base tools

  • Fully test covered

  • All raised exceptions now are based on Any2Error to help users catch them

Contributors

By order of contribution date:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

any2csv-0.3.4.zip (9.4 kB view details)

Uploaded Source

any2csv-0.3.4.tar.gz (6.2 kB view details)

Uploaded Source

Built Distributions

any2csv-0.3.4-py2.7.egg (6.8 kB view details)

Uploaded Source

any2csv-0.3.4-py2-none-any.whl (4.7 kB view details)

Uploaded Python 2

File details

Details for the file any2csv-0.3.4.zip.

File metadata

  • Download URL: any2csv-0.3.4.zip
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for any2csv-0.3.4.zip
Algorithm Hash digest
SHA256 67436e7696fb9c777c51969cf8abfc4dca17f5fc494e032a6612dbcfde77caf0
MD5 583405ffcb7ef9e6cd935bdef44b89de
BLAKE2b-256 19298cf0f61dabc8c2b2408b4fd64cf069dbf129d96027f404911e65a1f8ba79

See more details on using hashes here.

File details

Details for the file any2csv-0.3.4.tar.gz.

File metadata

  • Download URL: any2csv-0.3.4.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for any2csv-0.3.4.tar.gz
Algorithm Hash digest
SHA256 abb99b22388a7f4c73393419f132c41a3a3b618bb6b7e499f0d62436bcf94191
MD5 0262a201322abc0e4002f90ec1e0a3f7
BLAKE2b-256 25c76678c8156f8c4cf0e8d289ada6c1fc0f8be14966bd2aae9d4cfa6a443bc0

See more details on using hashes here.

File details

Details for the file any2csv-0.3.4-py2.7.egg.

File metadata

  • Download URL: any2csv-0.3.4-py2.7.egg
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for any2csv-0.3.4-py2.7.egg
Algorithm Hash digest
SHA256 4ab61aed54dca161134458751527e69c30e4875751f164dc3cdb44e8b8c9a010
MD5 6ad69984e35e806e10245a29c0ad7107
BLAKE2b-256 b3fdaff7091fab14035ef15f7f32448e39ff260fa6a5c71800c2d9db7c1760e1

See more details on using hashes here.

File details

Details for the file any2csv-0.3.4-py2-none-any.whl.

File metadata

File hashes

Hashes for any2csv-0.3.4-py2-none-any.whl
Algorithm Hash digest
SHA256 ad10bd34e64a89c527eb1b35537c5de0e231d2f4b3e3214a2dbaa273ef1f5c6d
MD5 274fbfb72de3c81be313e0153bcb3e17
BLAKE2b-256 3d191d914a9caddb0e023bb0426698b53f11fcc9fa4f6429b502d2cf3bdf14a2

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page