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


Groups > comp.lang.python > #98626

Re: using binary in python

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Michael Torrie <torriem@gmail.com>
Newsgroups comp.lang.python
Subject Re: using binary in python
Date Tue, 10 Nov 2015 17:19:14 -0700
Lines 47
Message-ID <mailman.232.1447201165.16136.python-list@python.org> (permalink)
References <mailman.164.1447060794.16136.python-list@python.org> <HbGdndTOtKc3EdzLnZ2dnUU7-YnOydjZ@giganews.com> <20151110212901.GA18609@z-sverige.nu>
Mime-Version 1.0
Content-Type text/plain; charset=windows-1252
Content-Transfer-Encoding 7bit
X-Trace news.uni-berlin.de zHXj1ZT/6vEfA3neYGBn5wbST7VMD0SY8bKxDDxAE5RQ==
Return-Path <torriem+gmail@torriefamily.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'anyway.': 0.04; 'binary': 0.05; 'encoded': 0.05; 'bytes.': 0.07; 'strings.': 0.07; 'bytes,': 0.09; 'encoding.': 0.09; 'exceptions,': 0.09; 'modes': 0.09; 'open()': 0.09; 'specifying': 0.09; 'subject:using': 0.09; 'python': 0.10; 'files.': 0.13; 'subject:python': 0.14; 'encoding': 0.15; 'file,': 0.15; '\\n,': 0.16; 'alot': 0.16; 'binary.': 0.16; 'bitwise': 0.16; 'corrupt': 0.16; 'decoding': 0.16; 'distinction': 0.16; 'doesnt': 0.16; 'from:addr:torriem': 0.16; 'from:name:michael torrie': 0.16; 'fully,': 0.16; 'marker': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'later': 0.16; 'bytes': 0.18; 'expanded': 0.18; 'of.': 0.18; 'language': 0.19; 'all,': 0.20; 'library': 0.20; '2015': 0.20; 'ascii': 0.22; 'converted': 0.22; 'file.': 0.22; 'trying': 0.22; 'wrote': 0.23; 'unix': 0.24; 'header:In-Reply-To:1': 0.24; 'mon,': 0.24; 'sort': 0.25; 'header:User-Agent:1': 0.26; "doesn't": 0.26; 'sense': 0.26; 'raw': 0.27; 'forgive': 0.27; 'translated': 0.27; 'larry': 0.29; 'windows,': 0.29; "i'd": 0.31; 'operations': 0.31; 'knows': 0.32; 'google,': 0.32; 'displayed': 0.33; 'open': 0.33; 'message-id:@gmail.com': 0.34; 'know.': 0.34; 'file': 0.34; 'worked': 0.34; 'world,': 0.35; 'text': 0.35; 'files,': 0.35; 'nov': 0.35; 'unicode': 0.35; 'expected': 0.35; 'but': 0.36; 'there': 0.36; 'notes': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'say': 0.37; 'thanks': 0.37; 'received:org': 0.37; 'wanted': 0.37; 'things': 0.38; 'difference': 0.38; 'data': 0.39; 'format': 0.39; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'some': 0.40; 'questions': 0.40; 'your': 0.60; 'back': 0.62; 'charset:windows-1252': 0.62; 'more': 0.63; 'between': 0.65; 'series': 0.65; 'python-list': 0.66; 'kent': 0.84; 'wish.': 0.84; 'worried': 0.84
X-Virus-Scanned amavisd-new at torriefamily.org
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0
In-Reply-To <20151110212901.GA18609@z-sverige.nu>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:98626

Show key headers only | View raw


On 11/10/2015 02:29 PM, kent nyberg wrote:
> On Mon, Nov 09, 2015 at 10:20:25PM -0800, Larry Hudson via Python-list wrote:
>> Your questions are somewhat difficult to answer because you misunderstand
>> binary.  The key is that EVERYTHING in a computer is binary.  There are NO
>> EXCEPTIONS, it's all binary ALL the time.  The difference comes about in how
>> this binary data is displayed and manipulated.  I want to emphasize, ALL the
>> DATA is binary.
>>
> 
> Thanks alot for taking the time.
> I get it now. I sort of, but not fully, misunderstood the conecpt of binary files. 
> The thing I was after; and the thing Im playing with now after a more succesfull time with google, 
> is writing more specific things to a file than just strings.
> English is not my native language so please forgive me, but
> I wanted to write specifc 16bit codes, and read them. And later play with bitwise operations on them. Sort of.
> It might not make sense at all, but hey..  it doesnt have to.
> Thanks anyway. :)

You're correct; it doesn't make that much sense.  If it were me I'd
write out my numbers in text format to the file. You can always read
them back in and covert them to a number.

Just a quick couple of notes on "binary" vs "text".  In the old days on
Windows, the difference between "binary" and "ascii" when it came to
file reading was simply the interpretation of the end-of-line marker.
Whenever you wrote out a \n, it got silently converted to two bytes,
0x0d 0x0a.  If you were trying to write a jpeg file, for example, this
would corrupt things as you well know.  In the Unix world, we never
worried about such things because the end-of-line marker was simple
0x0a.  It was never translated and never was expanded silently.  So when
it came to how we worked with files, there was no difference between
binary and ascii modes as far as the C library open() was concerned.

Now with Python 3, we now again do have to think about the distinction
between "text" and "binary" when working with files.  If we want to open
a text file, we have to open it while specifying the expected text
encoding, whether that is UTF-8, UCS-2, UTF-16, or some other old and
esoteric encoding.  That is to say when Python reads from a text file,
there is always going to be a decoding process going on where text file
bytes are read in, and then converted into unicode characters.  When
writing a text file out, unicode characters have to be encoded into a
series of bytes.  If Python knows what encoding we want, then it can do
this automatically as we write to the file.

In Python 3 opening a file for binary will read in raw bytes and you can
manipulate them however you wish.

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

using binary in python kent nyberg <kent@z-sverige.nu> - 2015-11-08 16:27 -0500
  Re: using binary in python Jussi Piitulainen <harvesting@makes.email.invalid> - 2015-11-09 11:58 +0200
  Re: using binary in python Larry Hudson <orgnut@yahoo.com> - 2015-11-09 22:20 -0800
    Re: using binary in python Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-11-10 15:14 -0500
      Re: using binary in python mm0fmf <none@mailinator.com> - 2015-11-10 20:36 +0000
        Re: using binary in python Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-11-10 16:02 -0500
          OT: Re: using binary in python mm0fmf <none@mailinator.com> - 2015-11-10 22:17 +0000
        Re: using binary in python Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-11-10 21:22 +0000
        Re: using binary in python Random832 <random832@fastmail.com> - 2015-11-10 19:03 -0500
        Re: using binary in python Random832 <random832@fastmail.com> - 2015-11-10 19:04 -0500
      Re: using binary in python Larry Hudson <orgnut@yahoo.com> - 2015-11-10 19:53 -0800
    Re: using binary in python Random832 <random832@fastmail.com> - 2015-11-10 15:44 -0500
    Re: using binary in python kent nyberg <kent@z-sverige.nu> - 2015-11-10 16:29 -0500
      Re: using binary in python Christian Gollwitzer <auriocus@gmx.de> - 2015-11-11 22:32 +0100
    Re: using binary in python Michael Torrie <torriem@gmail.com> - 2015-11-10 17:19 -0700

csiph-web