Reputation: 2801
I have a binary file that was created in Fortran consisting of integer values as records. I want to read these into Python, edit them as lists and save them back to binary as np-arrays. For some reason, however, Python inserts an additional "0" after every record in the file. I guess this is what they call "padding", right? How do I suppress this?
Here's a standalone example:
import numpy as np
content = np.array(range(20))
# Write:
with open('D:/bin_test.dat', 'wb') as write_binary:
write_binary.writelines(content)
# Read:
with open('D:/bin_test.dat', 'rb') as read_binary:
content = np.fromfile(read_binary, dtype=np.int16)
print content
Out:
[ 0 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 10 0 11
0 12 0 13 0 14 0 15 0 16 0 17 0 18 0 19 0]
If I read the Fortran binary file via np.fromfile
and save it back to binary directly, it works just fine. That's why I guess the problem occurs after conversion from list to numpy array.
Thanks!
Upvotes: 2
Views: 1468
Reputation: 114811
Check content.dtype
. It looks like it is np.int32
, which is generally the default integer type on Windows. You are writing 32 bit integers, but then trying to read them back as 16 bit integers. So every other value in the result is 0.
Upvotes: 2