[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

writing to a binary file without intervening spaces

Larry

3/17/2008 6:37:00 AM

Dear all,

I need to write integer values to a binary file that will be read by
another application, specifically ENVI. I want to write these values
without intervening spaces between values. For example:
My data is a = [23, 45, 56, 255].
My desire output is: 234556255, of course in binary file
representation already.

I tried to make one using write after pack method of struct module,
but because of spaces I got incorrect results. ENVI asks what data
type I have and from that gets a sequence of bytes to "form" the data.
With spaces, I think this gave me the problem.

Thanks.

2 Answers

Dennis Lee Bieber

3/17/2008 7:25:00 AM

0

On Sun, 16 Mar 2008 23:37:04 -0700 (PDT), Larry
<larry.cebuala@gmail.com> declaimed the following in comp.lang.python:

> Dear all,
>
> I need to write integer values to a binary file that will be read by
> another application, specifically ENVI. I want to write these values
> without intervening spaces between values. For example:
> My data is a = [23, 45, 56, 255].
> My desire output is: 234556255, of course in binary file
> representation already.
>
That... is a string of numeric characters...

I'd expect a "binary" of those values to be (in hex
representation!):

172D38FF (presuming 8-bit values)

> I tried to make one using write after pack method of struct module,
> but because of spaces I got incorrect results. ENVI asks what data
> type I have and from that gets a sequence of bytes to "form" the data.
> With spaces, I think this gave me the problem.
>
I don't know "ENVI"... Presuming your input is that list, and you
need a sequence of hex values

hex = "".join(["%2.2X" % n for n in a])



> Thanks.
--
Wulfraed Dennis Lee Bieber KD6MOG
wlfraed@ix.netcom.com wulfraed@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-asst@bestiaria.com)
HTTP://www.bestiaria.com/

Paul Rubin

3/17/2008 8:29:00 AM

0

Larry <larry.cebuala@gmail.com> writes:
> My data is a = [23, 45, 56, 255].
> My desire output is: 234556255, of course in binary file
> representation already.
>
> I tried to make one using write after pack method of struct module,
> but because of spaces I got incorrect results.

struct.pack works for me:

Python 2.4.4 (#1, Oct 23 2006, 13:58:00)
>>> import struct, os
>>> x = struct.pack('BBBB', 23, 45, 56, 255)
>>> len(x)
4
>>> f = open('/tmp/foo','w'); f.write(x); f.close()
>>> os.system("ls -l /tmp/foo")
-rw------- 1 phr phr 4 Mar 17 01:27 /tmp/foo
>>>

You could also do the list-string conversion with the array module.