简体   繁体   English

将 numpy 阵列保存到 json

[英]Saving numpy array to json

Im trying to save a numpy array to a json but since a ndarray is not JSON serializable I am converting them to lists.我试图将 numpy 数组保存到 json 但由于 ndarray 不是 JSON 可序列化的,因此我将它们转换为列表。 My problem is that this consumes an excesive amount of RAM.我的问题是这会消耗过多的 RAM。 Is there any other lightweight method?还有其他轻量级的方法吗?

You can use numpyencoder :您可以使用numpyencoder

import numpy as np
import json
from numpyencoder import NumpyEncoder

numpy_data = np.array([0, 1, 2, 3])

print(json.dumps(numpy_data, cls=NumpyEncoder))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM