Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'help?': 0.03; '*not*': 0.05; 'bytes.': 0.07; 'mode,': 0.07; 'resource.': 0.07; 'terry': 0.07; 'python': 0.07; 'bytes,': 0.09; 'default.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'statement.': 0.09; 'utf-8': 0.09; 'pm,': 0.11; 'this:': 0.11; '>>>': 0.12; 'output': 0.12; 'binary': 0.14; 'wrote:': 0.14; '(string': 0.16; '3.2)': 0.16; 'emacs': 0.16; 'emacs,': 0.16; 'guys,': 0.16; 'reedy': 0.16; 'subject:unicode': 0.16; 'url:unicode': 0.16; 'bytes': 0.19; 'jan': 0.22; 'header:In-Reply- To:1': 0.22; 'specified': 0.22; 'stored': 0.25; "i'm": 0.26; 'instead': 0.26; 'changed': 0.27; 'looks': 0.28; 'thanks': 0.29; 'assuming': 0.29; 'unicode': 0.29; 'confused': 0.31; 'url:articles': 0.31; 'does': 0.31; 'to:addr:python-list': 0.32; '...': 0.32; "i've": 0.33; 'character': 0.33; 'using': 0.34; 'header:X-Complaints-To:1': 0.34; 'file.': 0.34; 'got': 0.34; 'difference': 0.35; 'file': 0.35; 'characters': 0.35; 'open': 0.35; 'header:User-Agent:1': 0.35; 'some': 0.37; 'either': 0.37; 'sequence': 0.38; 'steven': 0.38; 'but': 0.38; 'sign': 0.38; 'so,': 0.38; 'used': 0.38; 'received:org': 0.38; 'to:addr:python.org': 0.39; 'header:Mime-Version:1': 0.39; 'how': 0.39; 'header:Received:5': 0.40; 'done:': 0.84; 'here...': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: unicode by default Date: Thu, 12 May 2011 00:12:02 -0400 References: <4dcb50f8$0$29973$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Gmane-NNTP-Posting-Host: rain.gmane.org User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.17) Gecko/20110414 Lightning/1.0b2 Thunderbird/3.1.10 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 62 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1305173553 news.xs4all.nl 81479 [::ffff:82.94.164.166]:35066 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:5194 On 5/11/2011 11:44 PM, harrismh777 wrote: > Steven D'Aprano wrote: >>> You need to understand the difference between characters and bytes. >> >> http://www.joelonsoftware.com/articles/Unicode.html >> >> is also a good resource. > > Thanks for being patient guys, here's what I've done: > >>>>> astr=3D"pound sign" >>>>> asym=3D" \u00A3" >>>>> afile=3Dopen("myfile", mode=3D'w') >>>>> afile.write(astr + asym) >> 12 >>>>> afile.close() > > > When I edit "myfile" with vi I see the 'characters' : > > pound sign =C2=A3 > > ... same with emacs, same with gedit ... > > > When I hexdump myfile I see this: > > 0000000 6f70 6375 2064 6973 6e67 c220 00a3 > This is *not* what I expected... well it is (little-endian) right up to= > the 'c2' and that is what is confusing me.... > I did not open the file with an encoding of UTF-8... so I'm assuming > UTF-16 by default (python3) so I was expecting a '00A3' little-endian a= s > 'A300' but what I got instead was UTF-8 little-endian 'c2a3' .... > > See my problem?... when I open the file with emacs I see the character > pound sign... same with gedit... they're all using UTF-8 by default. By= > default it looks like Python3 is writing output with UTF-8 as default..= =2E > and I thought that by default Python3 was using either UTF-16 or UTF-32= =2E > So, I'm confused here... also, I used the character sequence \u00A3 > which I thought was UTF-16... but Python3 changed my intent to 'c2a3' > which is the normal UTF-8... If you open a file as binary (bytes), you must write bytes, and they are = stored without transformation. If you open in text mode, you must write=20 text (string as unicode in 3.2) and Python will encode to bytes using=20 either some default or the encoding you specified in the open statement. = It does not matter how Python stored the unicode internally. Does this=20 help? Your intent is signalled by how you open the file. --=20 Terry Jan Reedy