Re: unicode by default
- From: MRAB <python@xxxxxxxxxxxxxxxxxxx>
- Date: Thu, 12 May 2011 03:31:18 +0100
On 12/05/2011 02:22, harrismh777 wrote:
John Machin wrote:You need to understand the difference between characters and bytes.(1) You cannot work without using bytes sequences. Files are byte
sequences. Web communication is in bytes. You need to (know / assume / be
able to extract / guess) the input encoding. You need to encode your
output using an encoding that is expected by the consumer (or use an
output method that will do it for you).
(2) You don't need to use bytes to specify a Unicode code point. Just use
an escape sequence e.g. "\u0404" is a Cyrillic character.
Thanks John. In reverse order, I understand point (2). I'm less clear on
If I generate a string of characters that I presume to be ascii/utf-8
(no \u0404 type characters) and write them to a file (stdout) how does
default encoding affect that file.by default..? I'm not seeing that
there is anything unusual going on... If I open the file with vi? If I
open the file with gedit? emacs?
Another question... in mail I'm receiving many small blocks that look
like sprites with four small hex codes, scattered about the mail...
mostly punctuation, maybe? ... guessing, are these unicode code points,
and if so what is the best way to 'guess' the encoding? ... is it coded
in the stream somewhere...protocol?
A string contains characters, a file contains bytes.
The encoding specifies how a character is represented as bytes.
In the Latin-1 encoding, the character "£" is represented by the byte 0xA3.
In the UTF-8 encoding, the character "£" is represented by the byte sequence 0xC2 0xA3.
In the ASCII encoding, the character "£" can't be represented at all.
The advantage of UTF-8 is that it can represent _all_ Unicode
characters (codepoints, actually) as byte sequences, and all those in
the ASCII range are represented by the same single bytes which the
original ASCII system used. Use the UTF-8 encoding unless you have to
use a different one.
A file contains only bytes, a socket handles only bytes. Which encoding
you should use for characters is down to protocol. A system such as
email, which can handle different encodings, should have a way of
specifying the encoding, and perhaps also a default encoding.
- Prev by Date: Re: unicode by default
- Next by Date: Re: checking if a list is empty
- Previous by thread: Re: unicode by default
- Next by thread: Re: unicode by default