Skip to content

Instantly share code, notes, and snippets.

@agalea91
Last active August 5, 2021 06:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agalea91/a4f11a9259dae05d88d6c46b837a520f to your computer and use it in GitHub Desktop.
Save agalea91/a4f11a9259dae05d88d6c46b837a520f to your computer and use it in GitHub Desktop.
Dump / load JSON line data.
import json
def dump_jsonl(data, output_path, append=False):
"""
Write list of objects to a JSON lines file.
"""
mode = 'a+' if append else 'w'
with open(output_path, mode, encoding='utf-8') as f:
for line in data:
json_record = json.dumps(line, ensure_ascii=False)
f.write(json_record + '\n')
print('Wrote {} records to {}'.format(len(data), output_path))
def load_jsonl(input_path) -> list:
"""
Read list of objects from a JSON lines file.
"""
data = []
with open(input_path, 'r', encoding='utf-8') as f:
for line in f:
data.append(json.loads(line.rstrip('\n|\r')))
print('Loaded {} records from {}'.format(len(data), input_path))
return data
@LaggAt
Copy link

LaggAt commented Aug 5, 2021

Thanks, I will have good use for that here:
https://github.com/LaggAt/actions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment