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


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

Weird list conversion

Started byhigh5storage@gmail.com
First post2015-12-13 11:45 -0800
Last post2015-12-13 12:59 -0700
Articles 9 — 7 participants

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


Contents

  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

#100387 — Weird list conversion

Fromhigh5storage@gmail.com
Date2015-12-13 11:45 -0800
SubjectWeird list conversion
Message-ID<5926ced0-5b86-41f2-81e8-55cc6f71b78c@googlegroups.com>
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!

[toc] | [next] | [standalone]


#100388

FromLaura Creighton <lac@openend.se>
Date2015-12-13 20:57 +0100
Message-ID<mailman.216.1450036662.12405.python-list@python.org>
In reply to#100387
In a message of Sun, 13 Dec 2015 11:45:19 -0800, high5storage@gmail.com 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 '='

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


#100390

FromKP <kai.peters@gmail.com>
Date2015-12-13 12:05 -0800
Message-ID<3193bf38-82c3-4538-b500-5e23158898e6@googlegroups.com>
In reply to#100388
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?

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


#100391

FromErik <python@lucidity.plus.com>
Date2015-12-13 20:28 +0000
Message-ID<mailman.218.1450038500.12405.python-list@python.org>
In reply to#100390
On 13/12/15 20:05, KP 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?

No, what is _returned_ is a list of one-byte strings.

When you call "print", then the list class's __repr__() method is called 
which in turn calls the contained objects' __repr__() methods in turn - 
it is those methods (in this case, they are all the string class's) 
which is deciding to render ASCII printable characters as just their 
value and ASCII non-printable characters using their hex notation.

E.

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


#100392

FromIan Kelly <ian.g.kelly@gmail.com>
Date2015-12-13 13:28 -0700
Message-ID<mailman.219.1450038568.12405.python-list@python.org>
In reply to#100390
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'

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


#100393

FromErik <python@lucidity.plus.com>
Date2015-12-13 20:31 +0000
Message-ID<mailman.220.1450038739.12405.python-list@python.org>
In reply to#100390
On 13/12/15 20:28, Erik wrote:
> When you call "print", then the list class's __repr__() method is called
> which in turn calls the contained objects' __repr__() methods in turn

I mean the __str__() method, not __repr__() in this case - however, the 
answer is otherwise the same.

E.

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


#100394

FromLarry Hudson <orgnut@yahoo.com>
Date2015-12-13 12:42 -0800
Message-ID<xa2dna2TzMigRfDLnZ2dnUU7-Y_OydjZ@giganews.com>
In reply to#100390
On 12/13/2015 12:05 PM, KP 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?
>
No.  The data is returned as raw bytes.
It's the print that is responsible for the way these bytes are _displayed_.

      -=- Larry -=-

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


#100395

FromChris Angelico <rosuav@gmail.com>
Date2015-12-14 08:09 +1100
Message-ID<mailman.221.1450040994.12405.python-list@python.org>
In reply to#100390
On Mon, Dec 14, 2015 at 7:31 AM, Erik <python@lucidity.plus.com> wrote:
> On 13/12/15 20:28, Erik wrote:
>>
>> When you call "print", then the list class's __repr__() method is called
>> which in turn calls the contained objects' __repr__() methods in turn
>
>
> I mean the __str__() method, not __repr__() in this case - however, the
> answer is otherwise the same.

It actually makes no difference; lists don't have __str__, and fall
back on object.__str__, which returns self.__repr__(). Inside
list.__repr__, the contained objects' reprs are combined. With lists,
you always get the repr. :)

ChrisA

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


#100389

FromIan Kelly <ian.g.kelly@gmail.com>
Date2015-12-13 12:59 -0700
Message-ID<mailman.217.1450036812.12405.python-list@python.org>
In reply to#100387
On Sun, Dec 13, 2015 at 12:45 PM,  <high5storage@gmail.com> wrote:
> 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'?

They're equivalent representations of the same thing.

py> '\x3D'
'='

[toc] | [prev] | [standalone]


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


csiph-web