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.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.3.zip (8.7 kB view details)

Uploaded Source

any2csv-0.3.3.tar.gz (5.5 kB view details)

Uploaded Source

Built Distributions

any2csv-0.3.3-py2.7.egg (6.1 kB view details)

Uploaded Source

any2csv-0.3.3-py2-none-any.whl (4.4 kB view details)

Uploaded Python 2

File details

Details for the file any2csv-0.3.3.zip.

File metadata

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

File hashes

Hashes for any2csv-0.3.3.zip
Algorithm Hash digest
SHA256 bbce45c0fbb716973278d18c41a77e9a309c785d738114a3f8719646c35d034a
MD5 ec49d14d08b35c32e758cc4bb39575c0
BLAKE2b-256 ac0b93435fb31e9e4a95e78d51ec7eedd6422afeeada47942b4a511530ef8e4a

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for any2csv-0.3.3.tar.gz
Algorithm Hash digest
SHA256 449c2afd87ab08605c801f23c202556951ff5ea79a39549145ed72f5938db4a1
MD5 994c63aa05c834a4708a65d14d5a98a2
BLAKE2b-256 21be1a57853a9856543bd81f4549d4efd9f549741af32cb000b2e8da4dceb0c0

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for any2csv-0.3.3-py2.7.egg
Algorithm Hash digest
SHA256 f2209021afbc878ff98391c8cf70c4c59d3d38c4b4ae50103646abf1792a865e
MD5 9c43ce3b8b23c805792040ee2b719c69
BLAKE2b-256 fdf936e4b3dfef5f5cbf3ae0dead39206ed78b7f89ae2545069bed94ad28c454

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for any2csv-0.3.3-py2-none-any.whl
Algorithm Hash digest
SHA256 3468ce057d3a8a63564634ad37e563de7d7120a4cda4ea5c2c99707e489e287f
MD5 5cf54a45b455f451b864a5eecd35b238
BLAKE2b-256 092cd31373a4fea6131f80c98c14cdd1b8c2e3ff8a3769bac00f3916ef5a0c53

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