打开二进制文件(32位有符号整数.DAT)文件到numpy的阵列 [英] Opening a binary (32 bit signed integer .dat) file into numpy arrays
问题描述
我有.dat文件有两个阵列,其中数据是交织的32位带符号整数。我想数据开成两个独立的numpy的数组。
我曾尝试使用numpy的FROMFILE将其打开。
导入numpy的是NPnewarray = np.fromfile('FILE.DAT',DTYPE = INT)
打印newarray
从我的文件,这个打印
[83886080 16777216 251658240 ...,0 50331648 16777216]
这是奇怪,因为我知道这两个数组应该开始像
[1 0 0 ...]
[15 5 11 ...]
根据我的交错数据我期待上述code给我1阵列,它看起来像
了解 [1 15 0 5 ...]
有谁知道我要去哪里错了吗?我可以张贴的文件,如果它会有所帮助。
尝试:
数据= np.fromfile('FILE.DAT',DTYPE = np.int32)
ARR1 =数据[:2]
ARR2 =数据[1:2]
或
数据= np.memmap('FILE.DAT',DTYPE = np.int32,模式='R')
ARR1 =数据[:2]
ARR2 =数据[1:2]
I have a 32 bit signed integer .dat file with two arrays where the data is interleaved. I want to open the data into two separate numpy arrays.
I have tried to open it using numpy 'fromfile'.
import numpy as np
newarray = np.fromfile('file.dat',dtype=int)
print newarray
From my file, this prints
[ 83886080 16777216 251658240 ..., 0 50331648 16777216]
Which is odd because I know the two arrays should start like
[ 1 0 0 ...]
[ 15 5 11 ...]
Based on my understanding of the interleaved data I was expecting the above code to give me 1 array which looked something like
[ 1 15 0 5 ...]
Does anyone know where I'm going wrong? I can post the file if it would help.
Try:
data = np.fromfile('file.dat', dtype=np.int32)
arr1 = data[::2]
arr2 = data[1::2]
or
data = np.memmap('file.dat', dtype=np.int32, mode='r')
arr1 = data[::2]
arr2 = data[1::2]
这篇关于打开二进制文件(32位有符号整数.DAT)文件到numpy的阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!