9
|
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
|