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.?
Asked
Active
Viewed 4,115 times
1
-
What do you mean *"integer values directly as integers"*?! – jonrsharpe Aug 05 '15 at 13:16
-
I mean that if I try to store 5 in .txt file, it will be stored as a character, not integer. – insanePi Aug 05 '15 at 13:20
-
What's an *"integer"*, though? Are you talking about binary representation? How many bits per number? – jonrsharpe Aug 05 '15 at 13:21
-
Please show the work you have already done. – TheBlackCat Aug 06 '15 at 09:32
-
the integers are probably in a `numpy` array. You could use its serialization methods e.g., something like [`numpy.savez_compressed()`](http://docs.scipy.org/doc/numpy/reference/generated/numpy.savez_compressed.html#numpy.savez_compressed). Otherwise,see [reading struct in python from created struct in c](http://stackoverflow.com/q/17244488/4279) -- the writing is even simpler, just call `file.write(array_with_c_types)`. – jfs Aug 06 '15 at 23:25
1 Answers
1
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())

Brobin
- 3,241
- 2
- 19
- 35
-
it is probably inefficient for image data with millions of pixels. There are [other options](http://stackoverflow.com/questions/31833610/dat-file-in-python#comment51653849_31833610) – jfs Aug 06 '15 at 23:28
-
It probably is. The most efficient and compressed format is probably the image itself. – Brobin Aug 06 '15 at 23:37