view lemuriformes/read.py @ 17:4793f99b73e0

[lemuriformes] utility functions
author Jeff Hammel <k0scist@gmail.com>
date Sun, 10 Dec 2017 17:42:52 -0800
parents
children
line wrap: on
line source

"""
deserialization
"""

import csv
import json
from .cast import isstring


def dictreader(csv_file):
    """read csv file into list of dicts"""

    with open(csv_file) as f:
        reader = csv.DictReader(f)
        return [row for row in reader]


def read_list_of_dicts(path, format):
    """
    read a list of dicts (not enforced)

    format -- should be 'csv' or 'json'
    """

    assert format in ('csv', 'json')
    assert isstring(path)
    if format == 'csv':
        return dictreader(path)

    with open(path) as f:
        return json.load(f)