numpy.savetxt() 報錯 Mismatch between array dtype (‘object‘) and format specifier (‘%.18e‘)的解決方法

將數組存儲爲文件:

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')

解決方法:

  1. 可以把 數組中每個元素個數變成一樣(長度相同)
  2. 或者改用 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])]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章