Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save manisnesan/e146aa0f0831a6e0581afcba6978d717 to your computer and use it in GitHub Desktop.
Save manisnesan/e146aa0f0831a6e0581afcba6978d717 to your computer and use it in GitHub Desktop.
Read and Write jsonline
# Install ujson
from fastcore.utils import dumps
def write_jsonl(cases: list, filename: str)->None:
''' Write a list of dictionaries to a jsonl file. This uses fastcore.utils `dumps` to serialize the dictionary to a string with `ujson` module.
Install `ujson` for faster serialization.'''
with open(filename, 'w') as file:
for item in cases: file.write(dumps(item) + '\n')
def read_jsonl(fname)->list:
''' Read a jsonl file into a list of dictionaries. This uses `ujson` module for faster deserialization. Install `ujson` for faster deserialization.'''
import ujson
with open(fname, 'r') as file: return [ujson.loads(line) for line in file]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment