Read printed numpy array

后端 未结 2 1792
借酒劲吻你
借酒劲吻你 2020-12-17 23:11

Sometimes the printed numpy array is provide to share the data such as this post. So far, I converted that manually. But the array in the post is too big to con

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-17 23:31

    You can use re to treat the string and then create the array using eval():

     import re
     from ast import literal_eval
    
     import numpy as np
    
     a = """[[[ 0 1]
              [ 2 3]]]"""
     a = re.sub(r"([^[])\s+([^]])", r"\1, \2", a)
     a = np.array(literal_eval(a))
    

提交回复
热议问题