2013-08-24 309 views
1

在我的bin文件數據排列是01 02 03 04。閱讀寫入和讀取數據

data X = numpy.fromfile( ,dtype=uint32) 

X後變爲:

04 03 02 01... 

此外,當X就像01 02 03 04...並將其寫入使用X.tofile()到文件,文件內容變得04 03 02 01

我需要以這樣的方式編寫和加載數組,以便我可以按照相同的順序獲取它們,並瞭解有關問題的任何想法?

+0

是什麼問題? – lulyon

回答

3

您使用little-endian的處理器,所以字節順序會有所不同,我不是一個numpy的用戶,但嘗試:

>>> hex(numpy.fromfile('1.txt', dtype=numpy.dtype('>u4'))) 
'0x1020304L' 
>>> 

查看更多Data type objects (dtype),順便說一下,數據沒變更見:

>>> # we stored 01 02 03 04 
>>> numpy.uint32(0x01020304).tofile('1.txt') 
>>> 
>>> # we see 04 03 02 01 
>>> open('1.txt', 'r').read() 
'\x04\x03\x02\x01' 
>>> 
>>> # when you load it, it's the same data 
>>> hex(numpy.fromfile('1.txt', dtype=numpy.uint32)) 
'0x1020304L' 
>>>