I'am doing a project in python using OpenCV. I have to store a large amount of integer data(features of images in the database) in a separate file. I can use .txt file but it stores integer values as strings.
Is there any way that I can store integer values directly as integers in python like .dat file in MATLAB.?
解决方案
You can use struct to pack the integers in a bytes format and write them to a dat file.
With integers, this will result in a file that contains 4 bytes per integer, which would save a bit of space (over text format) if you have very large numbers. If you have smaller numbers, a csv format may be better.
import struct
data = [1,2,3,4,5,6,7,8,9]
with open('data.dat', 'wb') as data_file:
data_file.write(struct.pack('i'*len(data), *data))
Then to read it back in
with open('data.dat', 'rb') as data_file:
values = struct.unpack('i'*len(data), data_file.read())