List of integers

Steven D'Aprano steve at pearwood.info
Mon Dec 14 05:37:38 EST 2015


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




More information about the Python-list mailing list