Parses each jsonl line and yields it as a dictionary
(filename: str)
| 13 | |
| 14 | |
| 15 | def stream_jsonl(filename: str) -> Iterable[Dict]: |
| 16 | """ |
| 17 | Parses each jsonl line and yields it as a dictionary |
| 18 | """ |
| 19 | if filename.endswith(".gz"): |
| 20 | with open(filename, "rb") as gzfp: |
| 21 | with gzip.open(gzfp, 'rt') as fp: |
| 22 | for line in fp: |
| 23 | if any(not x.isspace() for x in line): |
| 24 | yield json.loads(line) |
| 25 | else: |
| 26 | with open(filename, "r", encoding='utf-8') as fp: |
| 27 | for line in fp: |
| 28 | if any(not x.isspace() for x in line): |
| 29 | yield json.loads(line) |
| 30 | |
| 31 | |
| 32 | def write_jsonl(filename: str, data: Iterable[Dict], append: bool = False): |
no outgoing calls
no test coverage detected