本文介绍了将JSON字符串解析为numpy数组的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有巨大的json对象,其中包含2D坐标列表,我需要将其转换为numpy数组进行处理.

I have huge json objects containing 2D lists of coordinates that I need to transform into numpy arrays for processing.

但是在np.array()之后再使用json.loads太慢了.

However using json.loads followed with np.array() is too slow.

有没有办法提高从json创建numpy数组的速度?

Is there a way to increase the speed of creation of numpy arrays from json?

import json
import numpy as np

json_input = '{"rings" : [[[-8081441.0, 5685214.0], [-8081446.0, 5685216.0], [-8081442.0, 5685219.0], [-8081440.0, 5685211.0], [-8081441.0, 5685214.0]]]}'

dict = json.loads(json_input)
numpy_2d_arrays = [np.array(ring) for ring in dict["rings"]]

我会采取任何解决方案!

I would take any solution whatsoever!

推荐答案

由于JSON语法与Python语法非常接近,因此建议您使用ast.literal_eval.可能会更快...

Since JSON syntax is really near to Python syntax, I suggest you to use ast.literal_eval. It may be faster…

import ast
import numpy as np

json_input = """{"rings" : [[[-8081441.0, 5685214.0],
                             [-8081446.0, 5685216.0],
                             [-8081442.0, 5685219.0],
                             [-8081440.0, 5685211.0],
                             [-8081441.0, 5685214.0]]]}"""

rings = ast.literal_eval(json_input)
numpy_2d_arrays = [np.array(ring) for ring in rings["rings"]]

尝试一下.告诉我们.

这篇关于将JSON字符串解析为numpy数组的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 07:01