1

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.?

5
  • What do you mean "integer values directly as integers"?! Commented Aug 5, 2015 at 13:16
  • I mean that if I try to store 5 in .txt file, it will be stored as a character, not integer. Commented Aug 5, 2015 at 13:20
  • What's an "integer", though? Are you talking about binary representation? How many bits per number? Commented Aug 5, 2015 at 13:21
  • Please show the work you have already done. Commented Aug 6, 2015 at 9:32
  • the integers are probably in a numpy array. You could use its serialization methods e.g., something like numpy.savez_compressed(). Otherwise,see reading struct in python from created struct in c -- the writing is even simpler, just call file.write(array_with_c_types). Commented Aug 6, 2015 at 23:25

1 Answer 1

2

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

2 Comments

it is probably inefficient for image data with millions of pixels. There are other options
It probably is. The most efficient and compressed format is probably the image itself.

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.