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!newsfeed3.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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'python,': 0.02; 'subject:: [': 0.04; 'output': 0.05; 'binary': 0.07; 'arrays': 0.09; 'converted': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; "'rb')": 0.16; 'cc:name:python list': 0.16; 'matlab': 0.16; 'numpy': 0.16; 'appropriate': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'trying': 0.19; 'file,': 0.19; 'pfxlen:0': 0.19; 'subject:] ': 0.20; 'import': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'cc:2**0': 0.24; 'right.': 0.26; 'header:In- Reply-To:1': 0.27; 'tried': 0.27; 'array': 0.29; 'converting': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'lines': 0.31; "skip:' 10": 0.31; 'file': 0.32; "can't": 0.35; 'december': 0.35; 'convert': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'library.': 0.36; 'similar': 0.36; 'skip:o 20': 0.38; 'follows:': 0.38; 'does': 0.39; 'skip:p 20': 0.39; 'read': 0.60; 'forum': 0.61; 'new': 0.61; "you're": 0.61; 'first': 0.61; 'back': 0.62; 'save': 0.62; 'skip:n 10': 0.64; 'to:addr:gmail.com': 0.65; 'direct': 0.67; 'oscar': 0.84; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=zlDtY0gbEubJIzpto/TSc1MJSy23Vi9PD5cGrMsnWN8=; b=UqCjehRN53q+yDHNbifTMxZYEl3o7goFRVkk3DiMTQ0TLdkg1HwOayRwKg6TcwR+Pw R8RxGX8SdVT3g4ytV+8DTOau09H3Qhs4k5irEHmGVhVWnOFr7LayUBwj1xfXUoCGfoL4 c+QbCluIRynGpUVecuGXAHFjqp3eaMo+k6ZJuatLBwFD9EQHOdJ9YS9j5U2VzKfgKhsT 0oWrR048SrBOWUoT1DOOFWdT5RWQpGbkkqayzsBViHjc3XnEUYO7EenKlHdgXc16KlgE gNFlc3c1mzgta0FkZzp+Q5WXMwMrMtmIE3vp4tT4zrZvc5b1YueqH1DlUiDT4oST9x2k 7fyw== X-Received: by 10.58.155.162 with SMTP id vx2mr1062021veb.46.1387279298762; Tue, 17 Dec 2013 03:21:38 -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: Oscar Benjamin Date: Tue, 17 Dec 2013 11:21:18 +0000 Subject: Re: [newbie] Saving binaries in a specific way To: Djoser Content-Type: text/plain; charset=ISO-8859-1 Cc: Python List 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: 38 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1387279307 news.xs4all.nl 2832 [2001:888:2000:d::a6]:41112 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:62180 On 16 December 2013 22:19, Djoser 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