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


Groups > comp.lang.python > #100398 > unrolled thread

List of integers

Started byKP <kai.peters@gmail.com>
First post2015-12-13 16:24 -0800
Last post2015-12-14 21:37 +1100
Articles 5 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  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

#100398 — List of integers

FromKP <kai.peters@gmail.com>
Date2015-12-13 16:24 -0800
SubjectList of integers
Message-ID<6faae197-594a-4447-b146-6f5e01185e26@googlegroups.com>

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!

[toc] | [next] | [standalone]


#100399

FromChris Angelico <rosuav@gmail.com>
Date2015-12-14 11:32 +1100
Message-ID<mailman.0.1450053181.2330.python-list@python.org>
In reply to#100398
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

[toc] | [prev] | [next] | [standalone]


#100405

FromKP <kai.peters@gmail.com>
Date2015-12-13 19:11 -0800
Message-ID<980bd362-3c0f-4449-b474-bc8b492bc1cf@googlegroups.com>
In reply to#100399
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!

[toc] | [prev] | [next] | [standalone]


#100410

FromTerry Reedy <tjreedy@udel.edu>
Date2015-12-14 04:56 -0500
Message-ID<mailman.5.1450086999.14916.python-list@python.org>
In reply to#100398
On 12/13/2015 7:24 PM, KP wrote:
>
>
> data = list(f.read(4))
> print data
>
> from a binary file might give

In 2.x, a binary file and a text file are not distinguished.

> ['\x10', '\x20', '\x12', '\x01']

If a 'binary' file yields strings, you must be using 2.x.

> How can I receive this instead?
> [0x10, 0x20, 0x12, 0x01]

Use python 3.

-- 
Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#100413

FromSteven D'Aprano <steve@pearwood.info>
Date2015-12-14 21:37 +1100
Message-ID<566e9bf4$0$1591$c3e8da3$5496439d@news.astraweb.com>
In reply to#100410
On Mon, 14 Dec 2015 08:56 pm, Terry Reedy wrote:

> On 12/13/2015 7:24 PM, KP wrote:
>>
>>
>> data = list(f.read(4))
>> print data
>>
>> from a binary file might give
> 
> In 2.x, a binary file and a text file are not distinguished.

I think what you mean is that, in Python 2, reading from a file returns a
byte string regardless of whether you open it in text mode or binary mode.
That part is true, but there are other differences between text files and
binary files (opened in the correct mode): binary mode is guaranteed to
return the actual bytes in the file, but opening it in text mode may
perform some platform-specific processing:

(1) \r or \r\n may be converted to \n

(2) Ctrl-Z may be interpreted as end-of-file

There may be other changes made as well.


 
>> ['\x10', '\x20', '\x12', '\x01']
> 
> If a 'binary' file yields strings, you must be using 2.x.
> 
>> How can I receive this instead?
>> [0x10, 0x20, 0x12, 0x01]
> 
> Use python 3.

True, except by default integers display in decimal, not hex:

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



-- 
Steven

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web