showing results for - "load jsonl"
Nico
19 Jul 2018
1# https://medium.com/@galea/how-to-love-jsonl-using-json-line-format-in-your-workflow-b6884f65175b
2import json
3
4def dump_jsonl(data, output_path, append=False):
5    """
6    Write list of objects to a JSON lines file.
7    """
8    mode = 'a+' if append else 'w'
9    with open(output_path, mode, encoding='utf-8') as f:
10        for line in data:
11            json_record = json.dumps(line, ensure_ascii=False)
12            f.write(json_record + '\n')
13    print('Wrote {} records to {}'.format(len(data), output_path))
14
15def load_jsonl(input_path) -> list:
16    """
17    Read list of objects from a JSON lines file.
18    """
19    data = []
20    with open(input_path, 'r', encoding='utf-8') as f:
21        for line in f:
22            data.append(json.loads(line.rstrip('\n|\r')))
23    print('Loaded {} records from {}'.format(len(data), input_path))
24    return data
queries leading to this page
load jsonl love jsonlload jsonl