Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #62108 > unrolled thread

[newbie] Saving binaries in a specific way

Started byDjoser <pedrovg000@gmail.com>
First post2013-12-16 14:19 -0800
Last post2013-12-17 10:54 -0800
Articles 8 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  [newbie] Saving binaries in a specific way Djoser <pedrovg000@gmail.com> - 2013-12-16 14:19 -0800
    Re: [newbie] Saving binaries in a specific way Chris Kaynor <ckaynor@zindagigames.com> - 2013-12-16 14:28 -0800
      Re: [newbie] Saving binaries in a specific way Djoser <pedrovg000@gmail.com> - 2013-12-16 14:35 -0800
    Re: [newbie] Saving binaries in a specific way Tim Chase <python.list@tim.thechases.com> - 2013-12-16 16:48 -0600
      Re: [newbie] Saving binaries in a specific way Djoser <pedrovg000@gmail.com> - 2013-12-16 15:30 -0800
        Re: [newbie] Saving binaries in a specific way rusi <rustompmody@gmail.com> - 2013-12-16 17:01 -0800
    Re: [newbie] Saving binaries in a specific way Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-12-17 11:21 +0000
      Re: [newbie] Saving binaries in a specific way Djoser <pedrovg000@gmail.com> - 2013-12-17 10:54 -0800

#62108 — [newbie] Saving binaries in a specific way

FromDjoser <pedrovg000@gmail.com>
Date2013-12-16 14:19 -0800
Subject[newbie] Saving binaries in a specific way
Message-ID<3dcea919-82ad-4e0a-a00a-fa04ba39b1dd@googlegroups.com>
Hi all,

I am new to this forum and also to Python, but I'm trying hard to understand it  better.
I need to create a binary file, but the first 4 lines must be in signed-Integer16 and all the others in signed-Integer32. I have a program that does that with Matlab and other with Mathematica, but I'm converting all for Python.

I tried first to convert the number to binary using 'bin(number'), than I removed the '0b' and converted to 'Int16' or 'Int32', but with this approach I can't save a binary file using 'bytearray(').

How can I do that?


Thanks in advance.

[toc] | [next] | [standalone]


#62109

FromChris Kaynor <ckaynor@zindagigames.com>
Date2013-12-16 14:28 -0800
Message-ID<mailman.4238.1387232927.18130.python-list@python.org>
In reply to#62108

[Multipart message — attachments visible in raw view] — view raw

On Mon, Dec 16, 2013 at 2:19 PM, Djoser <pedrovg000@gmail.com> wrote:

> Hi all,
>
> I am new to this forum and also to Python, but I'm trying hard to
> understand it  better.
> I need to create a binary file, but the first 4 lines must be in
> signed-Integer16 and all the others in signed-Integer32. I have a program
> that does that with Matlab and other with Mathematica, but I'm converting
> all for Python.
>
> I tried first to convert the number to binary using 'bin(number'), than I
> removed the '0b' and converted to 'Int16' or 'Int32', but with this
> approach I can't save a binary file using 'bytearray(')



How can I do that?
>


What you probably want is to use the struct module:
http://docs.python.org/3/library/struct.html

You probably also want the open method and file objects:
http://docs.python.org/3/library/functions.html#open

It would help if you provided information about which Python version you
are using, and exactly what you mean by "I can't save a binary file using
'bytearray(')". Do you get a traceback, if so, copy it in your message,
along with actual code that produces your problem (preferably simplified,
but not too far).

[toc] | [prev] | [next] | [standalone]


#62110

FromDjoser <pedrovg000@gmail.com>
Date2013-12-16 14:35 -0800
Message-ID<78898917-848b-4af8-915a-74df920330ec@googlegroups.com>
In reply to#62109
I'm using python 2.7.
If I understood correctly, using bytearray I will lost the information about the signed 16, 32, since it makes automatically the conversion.

Do you think that I can make the conversion as I proposed before or using struct and save with open()? 

[toc] | [prev] | [next] | [standalone]


#62114

FromTim Chase <python.list@tim.thechases.com>
Date2013-12-16 16:48 -0600
Message-ID<mailman.4241.1387234067.18130.python-list@python.org>
In reply to#62108
On 2013-12-16 14:19, Djoser wrote:
> I am new to this forum and also to Python, but I'm trying hard to
> understand it  better.

Welcome aboard!

> I need to create a binary file, but the first 4 lines must be in
> signed-Integer16 and all the others in signed-Integer32. I have a
> program that does that with Matlab and other with Mathematica, but
> I'm converting all for Python.

You seem to be conflating ideas here:  a binary file doesn't really
have "lines".  Do you mean "first 4 bytes"?  If so, then a
signed-Integer16 really only occupies 2 bytes, so you'd have to pad
it somehow.  That said, I suspect that the "struct" module will get
you what you want:

  from struct import pack
  header16bit = 31415
  data = list(range(10))
  with open('output.bin', 'wb') as f:
    f.write(pack('h', header16bit))
    for signed_32bit_number in data:
      f.write(pack('i', signed_32bit_number))
    f.write(other_stuff)

You might need to specify the byte-ordering, which you can do by
prefixing the "h" or "i" with ">", "<" or "=" s documented at [1]

-tkc

[1]
http://www.python.org/doc//current/library/struct.html





.

[toc] | [prev] | [next] | [standalone]


#62117

FromDjoser <pedrovg000@gmail.com>
Date2013-12-16 15:30 -0800
Message-ID<fffa6a25-9477-4fba-b50c-c75cf0e35abb@googlegroups.com>
In reply to#62114
Basically I have a .dat file, so I get some numbers and make a different conversion.
I'll try this struct script. I'm not used to it, but it seems to do what I want.

[toc] | [prev] | [next] | [standalone]


#62124

Fromrusi <rustompmody@gmail.com>
Date2013-12-16 17:01 -0800
Message-ID<dec54cfc-d3ee-43b1-9aca-4b8409be869e@googlegroups.com>
In reply to#62117
On Tuesday, December 17, 2013 5:00:14 AM UTC+5:30, Djoser wrote:
> Basically I have a .dat file, so I get some numbers and make a different conversion.
> 
> I'll try this struct script. I'm not used to it, but it seems to do what I want.

Construct is a very powerful utility for binary parsing and building
https://pypi.python.org/pypi/construct
http://construct.readthedocs.org/en/latest/

Though for your currently stated purpose struct should be enough

[toc] | [prev] | [next] | [standalone]


#62180

FromOscar Benjamin <oscar.j.benjamin@gmail.com>
Date2013-12-17 11:21 +0000
Message-ID<mailman.4277.1387279307.18130.python-list@python.org>
In reply to#62108
On 16 December 2013 22:19, Djoser <pedrovg000@gmail.com> wrote:
> Hi all,

Hi Djoser,

> I am new to this forum and also to Python, but I'm trying hard to understand it  better.
> I need to create a binary file, but the first 4 lines must be in signed-Integer16 and all the others in signed-Integer32. I have a program that does that with Matlab and other with Mathematica, but I'm converting all for Python.

If you're coming from Matlab/Mathematica to Python you will likely
want to use the numpy library. This provides an array type that is
similar to Matlab arrays.

> I tried first to convert the number to binary using 'bin(number'), than I removed the '0b' and converted to 'Int16' or 'Int32', but with this approach I can't save a binary file using 'bytearray(').

Using numpy you can do this as follows:

import numpy as np

# Create arrays with the appropriate numeric types
first_numbers = np.array([12, -2, 10, -1], np.int16)
other_numbers = np.array([123, 123, 432, 543, 654, 654], np.int32)

# Output direct to binary file
with open('outputfile.bin', 'wb') as fout:
    first_numbers.tofile(fout)
    other_numbers.tofile(fout)

# Read back in from binary file
with open('outputfile.bin', 'rb') as fin:
    first_numbers_read = np.fromfile(fin, np.int16, count=4)
    other_numbers_read = np.fromfile(fin, np.int32)

# Print the data that we read back to check it's right.
print(first_numbers_read)
print(other_numbers_read)


Oscar

[toc] | [prev] | [next] | [standalone]


#62230

FromDjoser <pedrovg000@gmail.com>
Date2013-12-17 10:54 -0800
Message-ID<4c36e605-ea16-4986-bddf-466490c5355c@googlegroups.com>
In reply to#62180
Thank you.
With numpy it works perfectly. I thought it would lost the information about int32 and int16 with this approach.

Now I will try to make the script with struct too, but I'll need a bit more time to really understand. For me it's a new paradigm. But that's nice. :)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web