Mercurial > hg > Lemuriformes
comparison 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 |
comparison
equal
deleted
inserted
replaced
16:9b1bb9eee962 | 17:4793f99b73e0 |
---|---|
1 """ | |
2 deserialization | |
3 """ | |
4 | |
5 import csv | |
6 import json | |
7 from .cast import isstring | |
8 | |
9 | |
10 def dictreader(csv_file): | |
11 """read csv file into list of dicts""" | |
12 | |
13 with open(csv_file) as f: | |
14 reader = csv.DictReader(f) | |
15 return [row for row in reader] | |
16 | |
17 | |
18 def read_list_of_dicts(path, format): | |
19 """ | |
20 read a list of dicts (not enforced) | |
21 | |
22 format -- should be 'csv' or 'json' | |
23 """ | |
24 | |
25 assert format in ('csv', 'json') | |
26 assert isstring(path) | |
27 if format == 'csv': | |
28 return dictreader(path) | |
29 | |
30 with open(path) as f: | |
31 return json.load(f) |