0

I want to save numpy arrays to a jsonlines file. Using the code below:

import jsonlines
with jsonlines.open('output.jsonl', mode='w') as writer:
    writer.write({'a':np.array([2,3])})

with jsonlines.open('output.jsonl', mode='a') as writer:
    writer.write({'b':np.array([3,5])})

But I get this error:

TypeError: Object of type 'ndarray' is not JSON serializable

I wonder is there any way to save numpy arrays in jsonl format.

pouria babvey
  • 145
  • 2
  • 12
  • Save them as normal lists? [`numpy.ndarray.tolist`](https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.tolist.html). – erip Aug 22 '19 at 20:57
  • Thank you. I used the code above to scheme the problem. Actually, my arrays are a large list of numbers with dtype=float32. With converting them to lists, I don't miss any precision on my data, right? – pouria babvey Aug 22 '19 at 21:06

0 Answers0