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.5.zip (9.6 kB view details)

Uploaded Source

any2csv-0.3.5.tar.gz (6.5 kB view details)

Uploaded Source

Built Distributions

any2csv-0.3.5-py2.7.egg (7.2 kB view details)

Uploaded Source

any2csv-0.3.5-py2-none-any.whl (5.0 kB view details)

Uploaded Python 2

File details

Details for the file any2csv-0.3.5.zip.

File metadata

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

File hashes

Hashes for any2csv-0.3.5.zip
Algorithm Hash digest
SHA256 f8c57458ed875e4ab004708136e7af91caae25e36ac5eac59d883025ed2e3c1c
MD5 44ef841ffa82af6742e5ab4f990ea917
BLAKE2b-256 26f250a85cb68826e9762d62d90706ca4467489f3eee03b96cce1d4f2a028885

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for any2csv-0.3.5.tar.gz
Algorithm Hash digest
SHA256 e3d7ff744937224f860324234f4c01b2f585d956ac1b1e284b1c99cb235cfb90
MD5 3935d000c9b89da42930bf804f1cbdb1
BLAKE2b-256 d83ee3683419998cda8d84baa151c2a7c1d4ee5712787bf4d9ec19fa9a1f9837

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for any2csv-0.3.5-py2.7.egg
Algorithm Hash digest
SHA256 63480ac4160d68461107768f578c6caaa3b3f1378a6dc4b674cffa23a7d9cf02
MD5 342438f9c72c38cd280e3cd628844ba4
BLAKE2b-256 96c0897223d68a6783b37fb1892dfc4c99abcc8ab4c9fd6ad9367e1082fc469c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for any2csv-0.3.5-py2-none-any.whl
Algorithm Hash digest
SHA256 fb29abb037c302a9a43faa55470693f46a98d7ff47438918157ef7aaf9c1d9ca
MD5 6e232f9cbe775a09bdf90dc1bd68f8dc
BLAKE2b-256 7a894be1e207ab2d2e9625944995732bbec6451281cabee65734c5334ff2a8da

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