Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert .csv to .jsonl python

I have a .csv file which I would like to convert into a .jsonl file.

I found the Pandas to_json method:

df = pd.read_csv('DIRECTORY/texts1.csv', sep=';')
df.to_json ('DIRECTORY/texts1.json')

However, I am not aware of a function to turn it into a .jsonl format. How can I do this?

like image 984
Emil Avatar asked Mar 19 '26 13:03

Emil


1 Answers

This is probably a bit late, but I wrote a silly module called csv-jsonl that may help with this sort of thing.

>>> from csv_jsonl import JSONLinesDictWriter
>>> l = [{"foo": "bar", "bat": 1}, {"foo": "bar", "bat": 2}]
>>> with open("foo.jsonl", "w", encoding="utf-8") as _fh:
...     writer = JSONLinesDictWriter(_fh)
...     writer.writerows(l)
...

It extends the native csv module, so it's mostly familiar. Hope it helps.

like image 100
ccdoug Avatar answered Mar 22 '26 04:03

ccdoug