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


Groups > comp.lang.python > #100399

Re: List of integers

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Subject Re: List of integers
Date Mon, 14 Dec 2015 11:32:58 +1100
Lines 35
Message-ID <mailman.0.1450053181.2330.python-list@python.org> (permalink)
References <6faae197-594a-4447-b146-6f5e01185e26@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de jApkh8uTRvTyOgXKMVSiZglta1vbp8vGzanvAuqfSbVw==
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'received:209.85.223': 0.03; 'binary': 0.05; 'cc:addr:python-list': 0.09; 'subclass': 0.09; 'def': 0.13; '32,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'ord': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; '>>>': 0.20; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'am,': 0.23; 'dec': 0.23; 'this:': 0.23; 'header:In-Reply-To:1': 0.24; 'mon,': 0.24; 'message-id:@mail.gmail.com': 0.27; '14,': 0.27; "they'll": 0.29; 'print': 0.30; 'help!': 0.30; 'skip:_ 10': 0.32; 'class': 0.33; 'subject:List': 0.33; 'file': 0.34; 'that,': 0.34; 'received:google.com': 0.35; 'received:209.85': 0.36; 'subject:: ': 0.37; 'thanks': 0.37; "won't": 0.38; 'received:209': 0.38; 'data': 0.39; 'receive': 0.71; 'chrisa': 0.84; 'x):': 0.84; 'to:none': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=lwm3lPM+0TtgHlz+aFyoXa7A9qpM5H/lw5xaX24k/50=; b=EG4QWrpdx869oz8NMFPLfJwsHMd96U0N/9zFxFvWAi1htdrxHpy+Y20z7pXm3EBCI2 NQENnmLWPYnCymOge5FDGL2FW18xLOTCgkKrXbrjYMazk0aIVNmgqTlxdqLIfkQYJSPm +7U5VAXJWB1scwS1sX3LuTHVWRxYpuwgVRm5C4KM8y4ScEGHh1by15QiLzIXlrn+tTeX +kBuyBTDABVfFu11oHqwLQGJ5BNmft28V+d8rC6DRiUkqa9owz/YiO8/USanb2+C6IUA +J4SWTqoYulykftZgrE3QvacOzZBrqaZIHOl/ECScsDK1r69mlwiTg1JNT6HQKmY6M45 4lMg==
X-Received by 10.107.3.163 with SMTP id e35mr24552952ioi.157.1450053178628; Sun, 13 Dec 2015 16:32:58 -0800 (PST)
In-Reply-To <6faae197-594a-4447-b146-6f5e01185e26@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:100399

Show key headers only | View raw


On Mon, Dec 14, 2015 at 11:24 AM, KP <kai.peters@gmail.com> wrote:
> data = list(f.read(4))
> print data
>
> from a binary file might give
>
> ['\x10', '\x20', '\x12', '\x01']
>
>
> How can I receive this instead?
>
> [0x10, 0x20, 0x12, 0x01]
>
> Thanks for all help!

Try this:

data = [ord(x) for x in f.read(4)]

Note that it won't print out in hexadecimal.

>>> [0x10, 0x20, 0x12, 0x01]
[16, 32, 18, 1]

If you insist on that, try a subclass of int:

class ord(int):
    ord = ord
    def __new__(cls, x):
        return super().__new__(cls, cls.ord(x))
    def __repr__(self): return hex(self)

Then they'll come out in hex.

ChrisA

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


Thread

List of integers KP <kai.peters@gmail.com> - 2015-12-13 16:24 -0800
  Re: List of integers Chris Angelico <rosuav@gmail.com> - 2015-12-14 11:32 +1100
    Re: List of integers KP <kai.peters@gmail.com> - 2015-12-13 19:11 -0800
  Re: List of integers Terry Reedy <tjreedy@udel.edu> - 2015-12-14 04:56 -0500
    Re: List of integers Steven D'Aprano <steve@pearwood.info> - 2015-12-14 21:37 +1100

csiph-web