writing to a binary file without intervening spaces

L

Larry

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

Dennis Lee Bieber

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])


--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
P

Paul Rubin

Larry said:
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)
You could also do the list-string conversion with the array module.
 
C

castironpi

Larry said:
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.

Python 3.0a2 (r30a2:59405M, Dec 7 2007, 15:23:28) [MSC v.1500 32 bit
(Intel)] o
n win32
Type "help", "copyright", "credits" or "license" for more information.
bytes( [ 23, 45, 56, 255 ] ) b'\x17-8\xff'
a= _
f= open('temp.x','wb')
f.write( a ) 4
f= open('temp.x','rb')
f.read()
b'\x17-8\xff'
 
L

Larry

Thanks to all those who responded to this query.

It seems to me that Python always add intervening spaces between data
elements when writing to a file. Even with tostring() of numpy, array
elements get separated by space character. I like the output of
sys.stdout.write(file) to be writen as is to a binary file, but how?

I'll try your suggestions.

Good luck, especially to me! :)
 
P

Paul Rubin

Larry said:
It seems to me that Python always add intervening spaces between data
elements when writing to a file

It's just the print statement that does that.
 
L

Larry

It's just the print statement that does that.

I doubt that. I tried one small file, I opened it using the
application I mentioned, the display said like data, NaN, data, NaN...

I even went further to opening the file using notepad, and did a
search-and-replace for space characters. The result was what I
desired: data,data,data...
 
P

Paul Rubin

Larry said:
I even went further to opening the file using notepad, and did a
search-and-replace for space characters. The result was what I
desired: data,data,data...

In Windows, you also have to make sure to open binary files in binary
mode.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top