numpy.savetxt() 报错 Mismatch between array dtype (‘object‘) and format specifier (‘%.18e‘)的解决方法

    技术2022-07-11  86

    将数组存储为文件:

    import numpy as np a = np.array([[1,2,3],[1,2]]) np.savetxt('xxx.txt',a)

    报错:

    TypeError: Mismatch between array dtype ('object') and format specifier ('%.18e')

    解决方法:

    可以把 数组中每个元素个数变成一样(长度相同)或者改用 pickle 代替 numpy.savetxt() import numpy as np import pickle a = np.array([[1,2,3],[1,2]]) with open('uu.pk','wb') as file: pickle.dump(a,file) with open('uu.pk', 'rb') as file_1: b = pickle.load(file_1) print(type(b)) print(b)

    运行结果:

    <class 'numpy.ndarray'> [list([1, 2, 3]) list([1, 2])]
    Processed: 0.012, SQL: 9