Mercurial > hg > tvii
comparison tvii/read.py @ 73:16b3a11db8d2
add stub read functionality
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sun, 17 Dec 2017 13:27:24 -0800 |
parents | |
children | b56d329c238d |
comparison
equal
deleted
inserted
replaced
72:479b7c598a0e | 73:16b3a11db8d2 |
---|---|
1 """ | |
2 deserialization | |
3 """ | |
4 | |
5 # TODO: use `numerics`'s functionality | |
6 | |
7 import csv | |
8 from .types import string | |
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] |