Fastest way to parse JSON strings into numpy arrays

前端 未结 3 746
野趣味
野趣味 2021-02-05 15:08

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

However using json.loads followed wi

3条回答
  •  执念已碎
    2021-02-05 16:15

    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"]]
    

    Give it a try. And tell us.

提交回复
热议问题