annotate lemuriformes/columns.py @ 11:afc259799019

[CSV] add script to convert to SQL
author Jeff Hammel <k0scist@gmail.com>
date Sun, 10 Dec 2017 13:58:55 -0800
parents b69efcf85807
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9
b69efcf85807 [module] add columns
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 """
b69efcf85807 [module] add columns
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2 Load a CSV file by columns
b69efcf85807 [module] add columns
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3 """
b69efcf85807 [module] add columns
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4
b69efcf85807 [module] add columns
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5
b69efcf85807 [module] add columns
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6 import csv
b69efcf85807 [module] add columns
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7 from .decorators import fileobj
b69efcf85807 [module] add columns
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8
b69efcf85807 [module] add columns
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9
b69efcf85807 [module] add columns
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10 @fileobj
b69efcf85807 [module] add columns
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11 def read_columns(fp, type=dict):
b69efcf85807 [module] add columns
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12 """read columns from CSV `fp` as `type`"""
b69efcf85807 [module] add columns
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13
b69efcf85807 [module] add columns
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14 reader = csv.reader(fp)
b69efcf85807 [module] add columns
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15 columns = None
b69efcf85807 [module] add columns
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16
b69efcf85807 [module] add columns
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17 for row in reader:
b69efcf85807 [module] add columns
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18
b69efcf85807 [module] add columns
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19 if columns is None:
b69efcf85807 [module] add columns
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20 # read the header
b69efcf85807 [module] add columns
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21 header = row
b69efcf85807 [module] add columns
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22
b69efcf85807 [module] add columns
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23 # make a return value
b69efcf85807 [module] add columns
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24 columns = type([(key, []) for key in header])
b69efcf85807 [module] add columns
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25 continue
b69efcf85807 [module] add columns
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26
b69efcf85807 [module] add columns
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 for key, value in zip(header, row):
b69efcf85807 [module] add columns
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28 columns[key].append(value)
b69efcf85807 [module] add columns
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29
b69efcf85807 [module] add columns
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30 return columns