-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
36 lines (24 loc) · 1021 Bytes
/
utils.py
File metadata and controls
36 lines (24 loc) · 1021 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
'''A collection of misc. functions.'''
def read_csv(fn, delimiter='\t'):
'''Return a list of dictionaries with the contents of the file.
Assumes newline characters at the end of each line, which are trimmed.'''
objects = []
with open(fn, 'r') as inf:
iterator = inf.readlines()
header = iterator[0]
fields = header[:-1].split(delimiter)
for line in iterator[1:]:
objects.append({k: v for k, v in zip(fields, line[:-1].split(delimiter))})
return objects
def refactor_json_data(json_list, master_key):
'''Refactor a json list into a dictionary using
the key `key` from each dictionary in the list.
>>> refactor_json_data([{'id': 1, 'data': 'stuff'},
{'id': 2, 'data': 'things}], 'id')
{1: {'data': 'stuff'}, 2: {'data': 'things'}}
This is useful for fast lookups.'''
out_dict = {}
for item in json_list:
uid = item.pop(master_key)
out_dict[uid] = item
return out_dict