Skip to main content

Maps hierarchical dictionaries to flat structures

Project description

dictmapper was created to help transforming a sequence of python dictionaries to tabular format and specifically to transform json documents to be exported in csv fromat.

Usage

Create a mapper:

from dictmapper import Mapper, Mapping

class UserMapper(Mapper):

    user_id = Mapping('user_id')
    email = Mapping('email')
    Name = Mapping(lambda u: '%s %s' % (u['first_name'], u['last_name']), name='User Name')
    nickname = Mapping('nickname', default='N/A')
    street = Mapping('address/street')
    city = Mapping('address/city')
    joined_at = Mapping('joined_at', transform=lambda d: d.strftime('%Y-%m-%d'))

Sample input:

users_docs = [
    {
        'user_id': '1000001',
        'first_name': 'Test',
        'last_name': 'User',
        'email': 'user@test.com',
        'address': {
            'street': 'Example Road',
            'city': 'Emerald City',
        },
        'joined_at': datetime.now(),
    },
    {
        'user_id': '1000002',
        'first_name': 'Example',
        'last_name': 'Member',
        'nickname': 'exampy',
        'email': 'example@member.com',
        'address': {
            'street': 'Sample Road',
            'city': 'Emerald City',
        },
        'joined_at': datetime.now(),
    }
]

Output:

>>> mapper = UserMapper()
>>> mapper.headers()
['User id', 'Email', 'Name', 'Nickname', 'Street', 'City', 'Joined at']
>>> res = mapper.map(users_docs)
>>> res
[['1000001',
  'user@test.com',
  'Test User',
  'N/A',
  'Example Road',
  'Emerald City',
  '2012-03-18'],
 ['1000002',
  'example@member.com',
  'Example Member',
  'exampy',
  'Sample Road',
  'Emerald City',
  '2012-03-18']]

Export to csv

I recommend using the excellent tablib:

import tablib
data = tablib.Dataset(*res, headers=mapper.headers())
data.csv

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

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

dictmapper-0.1.linux-x86_64.tar.gz (4.3 kB view details)

Uploaded Source

File details

Details for the file dictmapper-0.1.linux-x86_64.tar.gz.

File metadata

File hashes

Hashes for dictmapper-0.1.linux-x86_64.tar.gz
Algorithm Hash digest
SHA256 905cffc01412872591d3742e0901d93bb24eeb248a73f2a8cfd46fc337e06b88
MD5 8e110cf9af80dd80e1bcc395cdd4e7b9
BLAKE2b-256 dff0606c6b6195068b4ad0ca980c2081c291019439933660f641a6527fe01086

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