我正在嘗試將 Matlab 代碼轉換為二進制文件的 Python。請原諒我是這門語言的新手。Matlab:fileID = fopen('file_name.bin','r');DC = fread(fileID,'single','b');Python:import numpy as npwith open('Duty_Cycle.bin','rb') as fid: data_array = np.fromfile(fid, np.float32, dtype = '>u4')print(data_array)結果:TypeError: argument for fromfile() given by name ('dtype') and position (2)我該如何解決這個錯誤?
1 回答

慕桂英3389331
TA貢獻2036條經驗 獲得超8個贊
的簽名fromfile
是
fromfile(file, dtype=float, count=-1, sep='', offset=0)
通過指定第二個位置參數np.float32
和關鍵字 argument dtype='>u4'
,您兩次給出了相同的參數,因此出現了錯誤。文檔不是很清楚,但您可以只使用字符串規范來指定類型和字節順序。
data_array = np.fromfile(fid, dtype='>u4')
添加回答
舉報
0/150
提交
取消