0

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.

3
  • @martineau Sorry I should have mentioned that I tried this and got an array [ 83886080 16777216 251658240 ..., 0 50331648 16777216], so that didn't appear to work. Note also that I'd prefer this as two arrays also. Commented Jan 9, 2015 at 4:08
  • @martineau Oh wow I misread the information. I'll edit it now. It is indeed a 32 bit signed integer file (it was being loaded into a floating point array in a different program). How would I go about reading it into the array myself? Commented Jan 9, 2015 at 4:24
  • 1
    Try dtype=np.int32. Commented Jan 9, 2015 at 5:39

1 Answer 1

1

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]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.