Method saves list of dicts into jsonl file. :param data: (list) list of dicts to be stored, :param filename: (str) path to the output file. If suffix .jsonl is not given then methods appends .jsonl suffix into the file. :param compress: (bool) should file be compressed into
(data_list: list, filename: str, compress: bool = True)
| 13 | from typing import * |
| 14 | |
| 15 | def dicts_to_jsonl(data_list: list, filename: str, compress: bool = True) -> None: |
| 16 | """ |
| 17 | Method saves list of dicts into jsonl file. |
| 18 | :param data: (list) list of dicts to be stored, |
| 19 | :param filename: (str) path to the output file. If suffix .jsonl is not given then methods appends |
| 20 | .jsonl suffix into the file. |
| 21 | :param compress: (bool) should file be compressed into a gzip archive? |
| 22 | """ |
| 23 | sjsonl = '.jsonl' |
| 24 | sgz = '.gz' |
| 25 | # Check filename |
| 26 | if not filename.endswith(sjsonl): |
| 27 | filename = filename + sjsonl |
| 28 | # Save data |
| 29 | |
| 30 | if compress: |
| 31 | filename = filename + sgz |
| 32 | with gzip.open(filename, 'w') as compressed: |
| 33 | for ddict in data_list: |
| 34 | jout = json.dumps(ddict) + '\n' |
| 35 | jout = jout.encode('utf-8') |
| 36 | compressed.write(jout) |
| 37 | else: |
| 38 | with open(filename, 'w') as out: |
| 39 | for ddict in data_list: |
| 40 | jout = json.dumps(ddict) + '\n' |
| 41 | out.write(jout) |
| 42 | |
| 43 | |
| 44 | def check_correctness( |