Binary

From DreamsteepWiki

Jump to: navigation, search

WRITEBINARY

import array

data = [1.2,2.3,3.4,4.5,3.4]

tmpfile = "C:/tmp.bin"
fileobj = open(tmpfile, mode='wb')
outvalues = array.array('f')
outvalues.fromlist( data )
outvalues.tofile(fileobj)
fileobj.close()


Class to write and read a binary file


import struct
import array

##############################################

class read_write_binary():

  def __init__(self):
    self.binaryfilename = 'C:/tmp.bin'
    
  def read(self):
    FH = open( self.binaryfilename, "rb" )
    start,stop = 0,struct.calcsize('f')
    Magic = struct.unpack("f", FH.read(4))[0]
    print Magic
    
    #bytes = struct.

  def write(self):
    data = [1.42]

    fileobj = open(self.binaryfilename, mode='wb')
    outvalues = array.array('f')
    outvalues.fromlist( data )
    outvalues.tofile(fileobj)
    fileobj.close()


##############################################


BIN=read_write_binary()
BIN.write()
BIN.read()
Personal tools