Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder1.xlned.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'python,': 0.02; '16,': 0.03; 'subject:: [': 0.04; 'that?': 0.05; 'binary': 0.07; 'converted': 0.09; 'objects:': 0.09; 'python': 0.11; '"i': 0.16; 'matlab': 0.16; 'using,': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'trying': 0.19; 'file,': 0.19; 'subject:] ': 0.20; 'email addr:gmail.com>': 0.22; 'to:name:python- list@python.org': 0.22; 'mon,': 0.24; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'converting': 0.30; 'dec': 0.30; 'message- id:@mail.gmail.com': 0.30; 'along': 0.30; "i'm": 0.30; 'code': 0.31; 'lines': 0.31; "skip:' 10": 0.31; 'produces': 0.31; 'struct': 0.31; 'file': 0.32; 'probably': 0.32; 'open': 0.33; 'url:python': 0.33; 'actual': 0.34; 'problem': 0.35; "can't": 0.35; 'received:209.85': 0.35; 'convert': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'version': 0.36; 'method': 0.36; 'url:org': 0.36; 'so,': 0.37; 'too': 0.37; 'received:209': 0.37; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'forum': 0.61; 'new': 0.61; 'url:3': 0.61; 'first': 0.61; 'save': 0.62; 'information': 0.63; '2013': 0.98 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:content-type; bh=6wAoB5hAOvT6/z1ZKyU+algtIXWc9bIPKIlsO1IGii8=; b=OEgQp1a+ggBpU4n1S4GB+yO7VeRkQmKy+d2CepuZeeZ4ajvcqHPg0kKj7aSGwZSyXb xd/7bmCKtP3weF1tR8DXOSMEaFJmMfSNB/PFh8k/WNIMBLVPnQViDoHBRmWHV9K+83Ei 29ciaqZBTlSmynDSxnABlPylfd3qPcxFQ4i5C7Q5X27SPwvgYBVAyi4SuXY8fRhfp+XH /pbgecd9kPTK1Cdn82DlCl9YF/adws6KVMzMsZjaLbQH0NCFZIF4KcEIWHvheo30yBwJ zBnTWZYQp9kxG66WTxYM0U3A7uCcuUzdWNtcvWVjRJjBrFX4Bp7vb0oXtBjrFKjTlf30 ZRyQ== X-Gm-Message-State: ALoCoQltBYAVt+znDlCXB0bHRqstCPvW1+8GxzssteHQkjeCBWJulmhoOVuHRTS/2XmJvGOB9ylh X-Received: by 10.182.158.71 with SMTP id ws7mr13745174obb.6.1387232919085; Mon, 16 Dec 2013 14:28:39 -0800 (PST) MIME-Version: 1.0 In-Reply-To: <3dcea919-82ad-4e0a-a00a-fa04ba39b1dd@googlegroups.com> References: <3dcea919-82ad-4e0a-a00a-fa04ba39b1dd@googlegroups.com> From: Chris Kaynor Date: Mon, 16 Dec 2013 14:28:18 -0800 Subject: Re: [newbie] Saving binaries in a specific way To: "python-list@python.org" Content-Type: multipart/alternative; boundary=089e01494a165cfe5604edae558b X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 88 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1387232927 news.xs4all.nl 2873 [2001:888:2000:d::a6]:56178 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:62109 --089e01494a165cfe5604edae558b Content-Type: text/plain; charset=ISO-8859-1 On Mon, Dec 16, 2013 at 2:19 PM, Djoser 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). --089e01494a165cfe5604edae558b Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
On M= on, 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 under= stand it =A0better.
I need to create a binary file, but the first 4 lines must be in signed-Int= eger16 and all the others in signed-Integer32. I have a program that does t= hat 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 'In= t32', but with this approach I can't save a binary file using '= bytearray(')=A0
=A0
How can I do that?


What = you probably want is to use the struct module:

You probably also want the open method and file objects= :

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, i= f so, copy it in your message, along with actual code that produces your pr= oblem (preferably simplified, but not too far).
--089e01494a165cfe5604edae558b--