Writing some floats in a file in an efficient way

ast none at gmail.com
Wed Feb 21 08:27:54 EST 2018


Hello

I would like to write a huge file of double precision
floats, 8 bytes each, using IEEE754 standard. Since
the file is big, it has to be done in an efficient
way.

I tried pickle module but unfortunately it writes
12 bytes per float instead of just 8.

Example:

import pickle

f = open("data.bin", "wb")
mypickler = pickle.Pickler(f)

mypickler.dump(0.0)
mypickler.dump(-0.0)

f.close()

Let's see what file data.bin contains now:

80 03 47
00 00 00 00 00 00 00 00
2E
80 03 47
80 00 00 00 00 00 00 00
2E

We see our 2 floats
00 00 00 00 00 00 00 00
which is the IEEE754 representation for 0.0 and
80 00 00 00 00 00 00 00
which is the IEEE754 representation for -0.0
yes, there are two 0 for floats, a positive and
a negative one ;-)

but there is a 3 bytes overhead 80 03 47 and an
ending byte 2E for each float. This is a 50% overhead.

Is there a way to write a float with only 8 bytes ?

Thx



More information about the Python-list mailing list