Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load jsonlines file with simple file read

Consider having the following code and a jsonl file,
there is a specific reason I don't read file with jsonlines.open() api, so please take this as a fact.

Reference for jsonlines package: https://jsonlines.readthedocs.io/en/latest/#jsonlines.Reader

import jsonlines

with open('example.jsonl', 'r') as jsonl_f:
    content = jsonl_f.read()

with jsonlines.Reader(content) as reader:
    lst = [obj for obj in reader]

example.jsonl content:

{"hello": "world"}
{"covid": "19"}

Error I get on lst= line:

 lst = [obj for obj in reader]
  File "../lib/python3.7/site-packages/jsonlines/jsonlines.py", line 204, in iter
    skip_empty=skip_empty)
  File "../lib/python3.7/site-packages/jsonlines/jsonlines.py", line 164, in read
    six.raise_from(exc, orig_exc)
  File "<string>", line 3, in raise_from
jsonlines.jsonlines.InvalidLineError: line contains invalid json: Expecting property name enclosed in double quotes: line 1 column 2 (char 1) (line 1)
like image 611
JavaSa Avatar asked Dec 03 '25 03:12

JavaSa


1 Answers

import jsonlines

with jsonlines.open('example.jsonl', 'r') as jsonl_f:
     lst = [obj for obj in jsonl_f]

The jsonl_f is the reader and can be used directly. It contains the lines in the json file.

like image 178
NargesooTv Avatar answered Dec 05 '25 18:12

NargesooTv



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!