[FRPythoneers] Reading Binary files

Jeffery D. Collins jcollins at boulder.net
Mon Jan 14 16:58:07 MST 2002


Check the function unpack() of the struct module.  You may be ablt to
add a line such as

	b = struct.unpack("f", a)

after a = calfile.read(4).  Note that endianess may come into play.
Do a help("struct") for more information.



On Mon, Jan 14, 2002 at 04:51:58PM -0700, J. Wayde Allen wrote:
> 
> I have a Basic program that reads a binary file, and need to port this to
> Python.  I've never really tried reading binary files with Python before
> so am having a bit of difficulty.
> 
> The lines of Basic that I've got are:
> 
>     OPEN cfile$ FOR BINARY AS #1
>     FOR	i = 1 TO 512
>     a$ = INPUT$(4,1)  'Get 4 byte binary data
>     f(i) = CVS(a$)    'Convert to single precision
>     NEXT i
>     CLOSE #1
> 
> What I've done for the Python version is:
> 
>    calfile = open (calfilename, 'rb')  #Open the file for binary read
>    for i in range(512):
>       a = calfile.read(4)
> 
> So how do I do the conversion to single precision?
> 
> - Wayde
>   (wallen at its.bldrdoc.gov)
> 
>    --------------------------------------------------------
>                         ISART 2002                          
>     International Symposium on Advanced Radio Technologies 
>       http://www.its.bldrdoc.gov/meetings/art/index.html  
>    --------------------------------------------------------
> 
> _______________________________________________
> This message sent by the FRPythoneers mailing list.
> Unsubscribe: echo unsubscribe | FRPythoneers-request at lists.community.tummy.com
> URL: http://lists.community.tummy.com/mailman/listinfo/frpythoneers

-- 
Jeffery Collins (http://www.boulder.net/~jcollins)



More information about the FRPythoneers mailing list