Mercurial > hg > tvii
annotate tvii/read.py @ 86:b56d329c238d
workaround namespace foolishess
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sun, 17 Dec 2017 14:04:45 -0800 |
parents | 16b3a11db8d2 |
children |
rev | line source |
---|---|
73 | 1 """ |
2 deserialization | |
3 """ | |
4 | |
5 # TODO: use `numerics`'s functionality | |
6 | |
7 import csv | |
86
b56d329c238d
workaround namespace foolishess
Jeff Hammel <k0scist@gmail.com>
parents:
73
diff
changeset
|
8 from ._types import string |
73 | 9 |
10 | |
11 def read(fp, type=float, separator=None): | |
12 """ | |
13 read CSV or space-separated value file `fp` | |
14 """ | |
15 | |
16 if isinstance(fp, string): | |
17 with open(fp) as f: | |
18 return read(f, type=type, separator=separator) | |
19 | |
20 try: | |
21 # whitespace separated values | |
22 return [[type(i) | |
23 for i in line.strip().split(separator)] | |
24 for line in fp.read().strip().splitlines()] | |
25 except ValueError: | |
26 # CSV | |
27 fp.seek(0) | |
28 kwargs = {} | |
29 if separator is not None: | |
30 kwargs['delimeter'] = separator | |
31 rows = [row for row in csv.reader(fp, **kwargs)] | |
32 return [[type(i) for i in row] | |
33 for row in rows] |