我正在阅读 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/