【问题标题】:Writing and Reading Data [closed]写入和读取数据 [关闭]
【发布时间】:2013-08-24 07:15:05
【问题描述】:

在我的 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

我需要以能够以相同顺序获取它们的方式编写和加载数组,关于问题可能是什么的任何想法?

【问题讨论】:

  • 问题是什么?

标签: python arrays python-2.7 file-io numpy


【解决方案1】:

你用的是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'
>>>

【讨论】:

    猜你喜欢
    • 2012-01-23
    • 2017-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多