[英]PYTHON: How do deal with data types after np.fromfile (binary format) correctly?
使用np.fromfile
和np.memmap
從任何二進制文件讀取數據后,分配的數據類型遇到各種問題。
我正在閱讀以下內容:
openfile = open(mypath,'rb')
openfile.seek(start_byte)
myvalue = np.fromfile(openfile, dtype = np.uint64, count=1)
print myvalue
返回:
myvalue = [1234]
myvalue
有8個字節,被解釋為ndarray,但我只想使用uint64值作為索引。
1)如何防止np.fromfile寫入ndarray?
如果我正在嘗試: myvalue = myvalue[0]
myvalue
完全丟失了它的數據類型。
2)為什么在我訪問第一個時, myvalue
丟失其數據類型
我必須對我的數組做類似的事情:
data.extend([myvalue for l in range(myvalue)])
嘗試再次分配數據類型: myvalue = myvalue[0].astype(np.uint64)
。 現在我得到:
self.data_array[count:count+myvalue,0] = data[count:count+myvalue]
TypeError: slice indices must be integers or None or have an __index__ method
3)這是怎么回事?
如果我將myvalue分配為: myvalue = myvalue[0].astype(np.int32)
數據解釋錯誤,我得到: -35566848567
等。
4)為什么由於讀入 myvalue = myvalue[0].astype(np.int32)
后,程序仍會錯誤地解釋 myvalue = myvalue[0].astype(np.int32)
不是 myvalue = myvalue[0].astype(np.uint64)
原諒可能的非答案,但以這種方式發布代碼更容易...
您為什么認為myvalue丟失其類型信息? 當我在這里嘗試(Python 2.7)時,它沒有這樣做:
>>> myvalue = np.array([1234], np.uint64)
>>> myvalue = myvalue[0]
>>> type(myvalue)
<type 'numpy.uint64'>
在其文檔中有關於使用fromfile
的警告:
注意事項-----不要依賴於
tofile
和fromfile
的組合來進行數據存儲,因為生成的二進制文件不是平台無關的。 特別是,不保存字節順序或數據類型信息。 可以使用save
和load
來以獨立於平台的.npy
格式存儲數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.