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 hashes)

Uploaded Source

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

Uploaded Source

Built Distributions

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

Uploaded Source

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

Uploaded Python 2

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