Parses each jsonl line and yields it as a dictionary
(filename: str)
| 65 | |
| 66 | |
| 67 | def stream_jsonl(filename: str) -> Iterable[Dict]: |
| 68 | """ |
| 69 | Parses each jsonl line and yields it as a dictionary |
| 70 | """ |
| 71 | if filename.endswith(".gz"): |
| 72 | with open(filename, "rb") as gzfp: |
| 73 | with gzip.open(gzfp, "rt") as fp: |
| 74 | for line in fp: |
| 75 | if any(not x.isspace() for x in line): |
| 76 | yield json.loads(line) |
| 77 | else: |
| 78 | with open(filename, "r") as fp: |
| 79 | for line in fp: |
| 80 | if any(not x.isspace() for x in line): |
| 81 | yield json.loads(line) |
| 82 | |
| 83 | |
| 84 | def write_jsonl(filename: str, data: Iterable[Dict], append: bool = False): |
no outgoing calls
no test coverage detected