Mercurial > hg > Lemuriformes
comparison lemuriformes/columns.py @ 9:b69efcf85807
[module] add columns
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sun, 10 Dec 2017 13:56:30 -0800 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
8:59c91bfb6c06 | 9:b69efcf85807 |
---|---|
1 """ | |
2 Load a CSV file by columns | |
3 """ | |
4 | |
5 | |
6 import csv | |
7 from .decorators import fileobj | |
8 | |
9 | |
10 @fileobj | |
11 def read_columns(fp, type=dict): | |
12 """read columns from CSV `fp` as `type`""" | |
13 | |
14 reader = csv.reader(fp) | |
15 columns = None | |
16 | |
17 for row in reader: | |
18 | |
19 if columns is None: | |
20 # read the header | |
21 header = row | |
22 | |
23 # make a return value | |
24 columns = type([(key, []) for key in header]) | |
25 continue | |
26 | |
27 for key, value in zip(header, row): | |
28 columns[key].append(value) | |
29 | |
30 return columns |