Writes an iterable of dictionaries to jsonl
(filename: str, data: Iterable[Dict], append: bool = False)
| 30 | |
| 31 | |
| 32 | def write_jsonl(filename: str, data: Iterable[Dict], append: bool = False): |
| 33 | """ |
| 34 | Writes an iterable of dictionaries to jsonl |
| 35 | """ |
| 36 | if append: |
| 37 | mode = 'ab' |
| 38 | else: |
| 39 | mode = 'wb' |
| 40 | filename = os.path.expanduser(filename) |
| 41 | if filename.endswith(".gz"): |
| 42 | with open(filename, mode) as fp: |
| 43 | with gzip.GzipFile(fileobj=fp, mode='wb') as gzfp: |
| 44 | for x in data: |
| 45 | gzfp.write((json.dumps(x) + "\n").encode('utf-8')) |
| 46 | else: |
| 47 | with open(filename, mode) as fp: |
| 48 | for x in data: |
| 49 | fp.write((json.dumps(x) + "\n").encode('utf-8')) |
no test coverage detected