Mercurial > hg > Lemuriformes
diff lemuriformes/read.py @ 17:4793f99b73e0
[lemuriformes] utility functions
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sun, 10 Dec 2017 17:42:52 -0800 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lemuriformes/read.py Sun Dec 10 17:42:52 2017 -0800 @@ -0,0 +1,31 @@ +""" +deserialization +""" + +import csv +import json +from .cast import isstring + + +def dictreader(csv_file): + """read csv file into list of dicts""" + + with open(csv_file) as f: + reader = csv.DictReader(f) + return [row for row in reader] + + +def read_list_of_dicts(path, format): + """ + read a list of dicts (not enforced) + + format -- should be 'csv' or 'json' + """ + + assert format in ('csv', 'json') + assert isstring(path) + if format == 'csv': + return dictreader(path) + + with open(path) as f: + return json.load(f)