Mercurial > hg > tvii
view 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 |
line wrap: on
line source
""" deserialization """ # TODO: use `numerics`'s functionality import csv from ._types import string def read(fp, type=float, separator=None): """ read CSV or space-separated value file `fp` """ if isinstance(fp, string): with open(fp) as f: return read(f, type=type, separator=separator) try: # whitespace separated values return [[type(i) for i in line.strip().split(separator)] for line in fp.read().strip().splitlines()] except ValueError: # CSV fp.seek(0) kwargs = {} if separator is not None: kwargs['delimeter'] = separator rows = [row for row in csv.reader(fp, **kwargs)] return [[type(i) for i in row] for row in rows]