Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder3.xlned.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'python,': 0.02; 'subject:: [': 0.04; 'binary': 0.07; 'bytes,': 0.09; 'data:': 0.09; '"="': 0.16; '-tkc': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'matlab': 0.16; 'somehow.': 0.16; 'wrote:': 0.18; 'module': 0.19; 'trying': 0.19; 'file,': 0.19; 'subject:] ': 0.20; 'import': 0.22; 'documented': 0.24; 'specify': 0.24; 'header:In-Reply-To:1': 0.27; '[1]': 0.29; "doesn't": 0.30; 'converting': 0.30; 'said,': 0.30; "i'm": 0.30; 'lines': 0.31; 'struct': 0.31; 'file': 0.32; 'url:python': 0.33; 'but': 0.35; 'really': 0.36; 'charset:us- ascii': 0.36; 'url:org': 0.36; 'so,': 0.37; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'forum': 0.61; 'new': 0.61; 'first': 0.61; 'here:': 0.62; 'pad': 0.84; 'received:50.22': 0.84; 'want:': 0.84 Date: Mon, 16 Dec 2013 16:48:56 -0600 From: Tim Chase To: python-list@python.org Subject: Re: [newbie] Saving binaries in a specific way In-Reply-To: <3dcea919-82ad-4e0a-a00a-fa04ba39b1dd@googlegroups.com> References: <3dcea919-82ad-4e0a-a00a-fa04ba39b1dd@googlegroups.com> X-Mailer: Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - boston.accountservergroup.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tim.thechases.com X-Get-Message-Sender-Via: boston.accountservergroup.com: authenticated_id: tim@thechases.com 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: 39 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1387234067 news.xs4all.nl 2855 [2001:888:2000:d::a6]:43382 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:62114 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 .