Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #100405
| Newsgroups | comp.lang.python |
|---|---|
| Date | 2015-12-13 19:11 -0800 |
| References | <6faae197-594a-4447-b146-6f5e01185e26@googlegroups.com> <mailman.0.1450053181.2330.python-list@python.org> |
| Message-ID | <980bd362-3c0f-4449-b474-bc8b492bc1cf@googlegroups.com> (permalink) |
| Subject | Re: List of integers |
| From | KP <kai.peters@gmail.com> |
On Sunday, 13 December 2015 16:33:20 UTC-8, Chris Angelico wrote: > On Mon, Dec 14, 2015 at 11:24 AM, KP <> 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 Thanks much!
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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