Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'encoding': 0.05; 'subject:Python': 0.06; 'encoded': 0.07; 'utf-8': 0.07; 'string': 0.09; 'ascii': 0.09; 'bytes.': 0.09; 'mixed': 0.09; 'non-text': 0.09; 'cc:addr:python-list': 0.11; 'jan': 0.12; 'changes': 0.15; '1900': 0.16; 'be:': 0.16; 'dict': 0.16; 'dictionary,': 0.16; 'expects': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'json,': 0.16; 'memo': 0.16; 'operation.': 0.16; 'stuff,': 0.16; 'subject:More': 0.16; 'subject:Unicode': 0.16; 'wrote:': 0.18; 'discussion': 0.18; 'things.': 0.19; 'example': 0.22; 'cc:addr:python.org': 0.22; 'bytes': 0.24; "shouldn't": 0.24; 'text,': 0.24; 'text.': 0.24; 'cc:2**0': 0.24; 'header:In- Reply-To:1': 0.27; 'chris': 0.29; 'am,': 0.29; 'character': 0.29; 'ideal': 0.29; 'involving': 0.30; 'message-id:@mail.gmail.com': 0.30; 'work.': 0.31; '(possibly': 0.31; 'piece': 0.31; 'subject:About': 0.31; 'file': 0.32; 'text': 0.33; "can't": 0.35; 'operations': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'are,': 0.36; 'method': 0.36; 'wrong': 0.37; 'fact': 0.38; 'pm,': 0.38; 'expect': 0.39; 'skip:. 10': 0.39; 'either': 0.39; 'subject:"': 0.60; 'most': 0.60; 'course': 0.61; 'simple': 0.61; 'first': 0.61; 'name': 0.63; 'field': 0.63; 'skip:n 10': 0.64; 'more': 0.64; 'between': 0.67; 'nobody': 0.68; 'age': 0.80; '0000': 0.84; '0300': 0.84; '4500': 0.84; 'ethan': 0.84; 'frustrating': 0.84; 'furman': 0.84; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=yzmgcctr9dUzVvP/0UOOnOJMmJ7ULOnPcf/UoGgDaJQ=; b=poCXX5YKJoA9ijmw8I0EZpJ5+Io9tCMZ6VlbnMCMHaRtzbECPZQs+eq4YC6NuTjTl+ WVD5hU1uCh83t2JG66guNgWjXq3bFu02AjNcxxtr6uCthftmvAwYwI/hB/EqsVG7io0N FRtHXKdh/cHD74vtzIuKMmwDaQlvl48xukrMI9IChIhIn9w29uIjOf3yhj+PUrobN/IH QQXFbM1BgiIt8ZMAheTw59DMtzvOnHexb+yogo/hpeT6t0hWY+lt6e3YNm3LHruCieLf RKnrZmhKd3jBK8qUxd3xpmJJYqvJ9G+401PRfvOSvfdktDcQqZNjW2GjbKPJO5Ygv81z SwyA== MIME-Version: 1.0 X-Received: by 10.68.162.66 with SMTP id xy2mr8037549pbb.46.1389023168825; Mon, 06 Jan 2014 07:46:08 -0800 (PST) In-Reply-To: <52CAC780.1010204@stoneleaf.us> References: <52C9FD02.3080109@stoneleaf.us> <52CAC780.1010204@stoneleaf.us> Date: Tue, 7 Jan 2014 02:46:08 +1100 Subject: Re: "More About Unicode in Python 2 and 3" From: Chris Angelico Cc: Python Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 39 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1389023179 news.xs4all.nl 2955 [2001:888:2000:d::a6]:51580 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:63289 On Tue, Jan 7, 2014 at 2:10 AM, Ethan Furman wrote: > On 01/05/2014 06:55 PM, Chris Angelico wrote: >> >> >> It can't be both things. It's either bytes or it's text. > > > Of course it can be: > > 0000000: 0372 0106 0000 0000 6100 1d00 0000 0000 .r......a....... > 0000010: 0000 0000 0000 0000 0000 0000 0000 0000 ................ > 0000020: 4e41 4d45 0000 0000 0000 0043 0100 0000 NAME.......C.... > 0000030: 1900 0000 0000 0000 0000 0000 0000 0000 ................ > 0000040: 4147 4500 0000 0000 0000 004e 1a00 0000 AGE........N.... > 0000050: 0300 0000 0000 0000 0000 0000 0000 0000 ................ > 0000060: 0d1a 0a ... > > And there we are, mixed bytes and ascii data. As I said earlier, my example > is minimal, but still very frustrating in that normal operations no longer > work. Incidentally, if you were thinking that NAME and AGE were part of the > ascii text, you'd be wrong -- the field names are also encoded, as are the > Character and Memo fields. That's alternating between encoded text and non-text bytes. Each individual piece is either text or non-text, not both. The ideal way to manipulate it would most likely be a simple decode operation that turns this into (probably) a dictionary, decoding both the structure/layout and UTF-8 in a single operation. But a less ideal (and more convenient) solution might be involving what's currently under discussion elsewhere: a (possibly partial) percent-formatting or .format() method for bytes. None of this changes the fact that there are bytes used to store/transmit stuff, and abstract concepts used to manipulate them. Just like nobody expects to be able to write a dict to a file without some form of encoding (pickle, JSON, whatever), you shouldn't expect to write a character string without first turning it into bytes. ChrisA