python - 为什么我们在这里需要字节顺序?

标签 python numpy endianness

我正在阅读 source-code它下载 zip 文件并将数据读入 numpy 数组。该代码假设适用于 macos 和 linux,这是我看到的代码片段:

def _read32(bytestream):
    dt = numpy.dtype(numpy.uint32).newbyteorder('>')
    return numpy.frombuffer(bytestream.read(4), dtype=dt)

此函数在以下上下文中使用:

with gzip.open(filename) as bytestream:
    magic = _read32(bytestream)

不难看出这里发生了什么,但我对 newbyteorder('>') 的目的感到困惑。我读了documentation ,并且知道字节顺序是什么意思,但不明白为什么开发人员添加了 newbyteorder(在我看来,这并不是真正需要的)。

最佳答案

那是因为下载的数据是大端格式,如源页面所述:http://yann.lecun.com/exdb/mnist/

All the integers in the files are stored in the MSB first (high endian) format used by most non-Intel processors. Users of Intel processors and other low-endian machines must flip the bytes of the header.

关于python - 为什么我们在这里需要字节顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33692321/

相关文章:

python - cats() 收到意外的关键字参数 'pk'

python - 多条件 np.extract

python - 如何使用 numpy.timedelta 添加(或减去)1 个月?

java - java中如何将字节转换为长整型

java - Java 语言规范中哪里定义了运行时字节序?

c++ - 这与字节序有什么关系吗?

python - 使用未实例化的类作为 namespace 来组织对象?

python - 子类化 Numpy 数组 - 传播属性

python - 运行最大的 numpy 数组值

python - Python 中的二阶导数 - scipy/numpy/pandas