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


Groups > comp.lang.python > #100392

Re: Weird list conversion

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Ian Kelly <ian.g.kelly@gmail.com>
Newsgroups comp.lang.python
Subject Re: Weird list conversion
Date Sun, 13 Dec 2015 13:28:40 -0700
Lines 34
Message-ID <mailman.219.1450038568.12405.python-list@python.org> (permalink)
References <5926ced0-5b86-41f2-81e8-55cc6f71b78c@googlegroups.com> <mailman.216.1450036662.12405.python-list@python.org> <3193bf38-82c3-4538-b500-5e23158898e6@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de 2mNT/BOjrvtPbMvPn3EcMgOeXvSox1SBgMlxqEj7wZKA==
Return-Path <ian.g.kelly@gmail.com>
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; 'received:209.85.223': 0.03; 'string.': 0.04; 'creighton': 0.09; 'repr': 0.09; 'python': 0.10; "'=',": 0.16; 'depart': 0.16; 'editor,': 0.16; 'eval': 0.16; 'hex': 0.16; 'nul': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'repr()': 0.16; 'wrote:': 0.16; 'byte': 0.18; 'bytes': 0.18; 'laura': 0.18; 'all,': 0.20; '2015': 0.20; 'ascii': 0.22; 'dec': 0.23; 'header:In-Reply-To:1': 0.24; 'subject:list': 0.26; 'message-id:@mail.gmail.com': 0.27; 'specifically': 0.28; '13,': 0.29; 'always,': 0.29; 'escaped': 0.29; 'notation': 0.29; 'print': 0.30; 'that.': 0.30; 'code': 0.30; 'help!': 0.30; 'probably': 0.31; 'december': 0.32; 'generally': 0.32; 'returned': 0.32; 'useful': 0.33; 'doubt': 0.33; 'rule': 0.33; 'file': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'necessary.': 0.35; "isn't": 0.35; 'according': 0.36; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'thanks': 0.37; 'requirement': 0.37; 'received:209': 0.38; 'means': 0.39; 'why': 0.39; 'data': 0.39; 'does': 0.39; 'to:addr:python.org': 0.40; 'skip:n 10': 0.62; 'escaping': 0.84; 'to:name:python': 0.84
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 :content-type; bh=7QBcJXpymszfzieFm9dhOoOngsIG/uD09a3dUWFaVgA=; b=0F2RYVwUbVJBDUgNVt2SI3+KAoNmGDm/1oZ6hum7K247Z9AlWbd0YCh1/I/VMoJb2y UJLD4K9+n+9mwVK1e1P9I311hA5q1DGD0OMUC8Qy7ferZj7zVHeNMkHv32oZFifJ/YPv sCyPsd2p1QOHpRxgNA9qaBv4r5+idxhafNP8QXttgxhmH/8yYkTjmqXCyp2xnIa6jti5 /6FocIMNQm8+TwHzYvLEEZ0eAyBdRfKzmkxdZqwY05oYtVP0xiUXwdh4HmXOD6VfPShw xzoLQ6ZrnbHo9DfuDhY2pTvxMwe7RNDVxBGXwM1eYWHKF0rEQCGZ3Ssxw30e1gMNCI7Y VTZw==
X-Received by 10.107.154.195 with SMTP id c186mr6521490ioe.111.1450038560217; Sun, 13 Dec 2015 12:29:20 -0800 (PST)
In-Reply-To <3193bf38-82c3-4538-b500-5e23158898e6@googlegroups.com>
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:100392

Show key headers only | View raw


On Sun, Dec 13, 2015 at 1:05 PM, KP <kai.peters@gmail.com> wrote:
> On Sunday, 13 December 2015 11:57:57 UTC-8, Laura Creighton  wrote:
>> In a message of Sun, 13 Dec 2015 11:45:19 -0800, KP writes:
>> >Hi all,
>> >
>> >      f = open("stairs.bin", "rb")
>> >      data = list(f.read(16))
>> >      print data
>> >
>> >returns
>> >
>> >['=', '\x04', '\x00', '\x05', '\x00', '\x01', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00']
>> >
>> >The first byte of the file is 0x3D according to my hex editor, so why does Python return '=' and not '\x3D'?
>> >
>> >As always, thanks for any help!
>>
>> 0x3d is the ascii code for '='
>
> I am aware of that - so is the rule that non-printables are returned in hex notation whereas printables come in their ASCII representation?

What you're seeing there is the repr() of the string. The rule is that
it should be a printable representation that can be passed to eval to
reconstruct the string. The printable requirement means that NUL
should always come out escaped as '\x00' or perhaps '\0', but for
printable bytes escaping isn't necessary. I don't know if there's a
requirement that the repr of '\x3D' be '=', but it's probably the most
generally useful and I doubt that any implementation would depart from
that.

If you specifically want hex representations, then you could use hex(ord(foo)).

py> hex(ord('='))
'0x3d'

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


Thread

Weird list conversion high5storage@gmail.com - 2015-12-13 11:45 -0800
  Re: Weird list conversion Laura Creighton <lac@openend.se> - 2015-12-13 20:57 +0100
    Re: Weird list conversion KP <kai.peters@gmail.com> - 2015-12-13 12:05 -0800
      Re: Weird list conversion Erik <python@lucidity.plus.com> - 2015-12-13 20:28 +0000
      Re: Weird list conversion Ian Kelly <ian.g.kelly@gmail.com> - 2015-12-13 13:28 -0700
      Re: Weird list conversion Erik <python@lucidity.plus.com> - 2015-12-13 20:31 +0000
      Re: Weird list conversion Larry Hudson <orgnut@yahoo.com> - 2015-12-13 12:42 -0800
      Re: Weird list conversion Chris Angelico <rosuav@gmail.com> - 2015-12-14 08:09 +1100
  Re: Weird list conversion Ian Kelly <ian.g.kelly@gmail.com> - 2015-12-13 12:59 -0700

csiph-web