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


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

Re: python 2.7 and unicode (one more time)

Started byChris Angelico <rosuav@gmail.com>
First post2014-11-21 03:46 +1100
Last post2014-11-21 03:46 +1100
Articles 1 — 1 participant

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

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: python 2.7 and unicode (one more time) Chris Angelico <rosuav@gmail.com> - 2014-11-21 03:46 +1100

#81088 — Re: python 2.7 and unicode (one more time)

FromChris Angelico <rosuav@gmail.com>
Date2014-11-21 03:46 +1100
SubjectRe: python 2.7 and unicode (one more time)
Message-ID<mailman.16028.1416501987.18130.python-list@python.org>
On Fri, Nov 21, 2014 at 3:32 AM, Peter Otten <__peter__@web.de> wrote:
> Chris Angelico wrote:
>
>> On Fri, Nov 21, 2014 at 2:40 AM, Peter Otten <__peter__@web.de> wrote:
>>> I think that you may get a Unicode/Encode/Error when you try to /decode/
>>> a unicode string is more confusing...
>>
>> Hang on a minute, what does it even mean to decode a Unicode string?
>
> Let's not get philosophical ;)

No, I'm quite serious. You encode Unicode text into bytes; you decode
bytes into text. You can also encode a floating-point value into
bytes, and decode bytes into a float. Or you could encode a large and
complex structure into bytes, using something like pickle or json, and
then decode those bytes later on. The pattern is always the same: the
abstract object with meaning to a human is encoded into a concrete
form that a computer can handle, and the concrete is decoded into the
abstract. If you're not good at sight-reading sheet music, you'll have
the same feeling of staring at the dots, decoding them one by one into
this abstract thing called "music", and then being able to work with
it.

When you try to decode a Unicode string, what happens is that Python 2
says "Oh, you're trying to do a byte-string operation on a Unicode
string... I'll quickly encode that to bytes for you, then do what you
asked". That's why you can get an *en*coding error when you asked to
*de*code - because both operations have to happen.

ChrisA

[toc] | [standalone]


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


csiph-web