我有以下的文本文件,file.txt的3行4列:重塑:不能重塑尺寸x的陣列分成形狀(X,Y)
0.0 0.0 0.0
0.0 0.0 10.0
15 10 2001 2995
我使用NP .loadtxt將其作爲數組讀取。 Loadtxt將其作爲一維數組讀取,我想將它轉換回3x4陣列,它的結構與文本文件中的結構相同。我試過
file = sys.argv[1] #I'm just reading it from the command line when executing the program
data = np.loadtxt(file, delimiter='\t', dtype = str)
print(data.shape, data)
data = data.reshape(3,4)
但收到以下錯誤:
(3,)
['0.0 0.0 0.0' '0.0 0.0 10.0' '15 10 2001 2995']
ValueError: cannot reshape array of size 3 into shape (3,4)
我已經編輯了形狀和錯誤之間的非相關信息。我怎樣才能將這個文本文件重塑爲3x4數組?它不必通過加載文本。我也嘗試使用np.genfromtxt以及無濟於事。
爲什麼你使用'delimiter =',''爲一個沒有用','分隔的文件? – user2357112
爲什麼你斷言輸入結構是3x4? – user2357112
修正了分隔符。這就是我在文本編輯器中打開文件時的樣子。 – dward4