view lemuriformes/columns.py @ 15:0d1b8bb1d97b

SQL + data related functionality
author Jeff Hammel <k0scist@gmail.com>
date Sun, 10 Dec 2017 17:16:52 -0800
parents b69efcf85807
children
line wrap: on
line source

"""
Load a CSV file by columns
"""


import csv
from .decorators import fileobj


@fileobj
def read_columns(fp, type=dict):
    """read columns from CSV `fp` as `type`"""

    reader = csv.reader(fp)
    columns = None

    for row in reader:

        if columns is None:
            # read the header
            header = row

            # make a return value
            columns = type([(key, []) for key in header])
            continue

        for key, value in zip(header, row):
            columns[key].append(value)

    return columns