MCPcopy Create free account
hub / github.com/PRIME-RL/PRIME / write_jsonl

Function write_jsonl

eval/utils/data.py:32–49  ·  view source on GitHub ↗

Writes an iterable of dictionaries to jsonl

(filename: str, data: Iterable[Dict], append: bool = False)

Source from the content-addressed store, hash-verified

30
31
32def 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'))

Callers 3

Calls 1

encodeMethod · 0.45

Tested by

no test coverage detected