Mercurial > hg > Lemuriformes
changeset 9:b69efcf85807
[module] add columns
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sun, 10 Dec 2017 13:56:30 -0800 |
parents | 59c91bfb6c06 |
children | ad1bf59eedb0 |
files | lemuriformes/columns.py |
diffstat | 1 files changed, 30 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lemuriformes/columns.py Sun Dec 10 13:56:30 2017 -0800 @@ -0,0 +1,30 @@ +""" +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