Path: csiph.com!usenet.pasdenom.info!gegeweb.org!newsfeed0.kamp.net!newsfeed.kamp.net!feeder1.cambriumusenet.nl!82.197.223.108.MISMATCH!feeder2.cambriumusenet.nl!feed.tweaknews.nl!194.109.133.83.MISMATCH!newsfeed.xs4all.nl!newsfeed4.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; 'example:': 0.03; '"as': 0.07; '*the': 0.09; 'escape': 0.09; 'function:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'sequences.': 0.09; 'python': 0.11; 'def': 0.12; 'windows': 0.15; '"w")': 0.16; '...,': 0.16; '2.7.2': 0.16; 'idle,': 0.16; 'non-ascii': 0.16; 'n\xc3\xb6tig': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'repr()': 0.16; 'subject:Unicode': 0.16; 'variants': 0.16; 'files.': 0.16; 'wrote:': 0.18; 'file,': 0.19; '>>>': 0.22; 'print': 0.22; 'this?': 0.23; 'header:User-Agent:1': 0.23; 'helper': 0.24; 'questions:': 0.24; 'stephen': 0.24; 'stick': 0.24; 'unicode': 0.24; 'decide': 0.24; 'question': 0.24; 'second': 0.26; 'header:X -Complaints-To:1': 0.27; 'characters': 0.30; '(which': 0.31; 'tuples': 0.31; 'file': 0.32; 'text': 0.33; 'objects': 0.35; "i'll": 0.36; 'similar': 0.36; 'should': 0.36; 'being': 0.38; 'skip:o 20': 0.38; 'displays': 0.38; 'to:addr:python-list': 0.38; 'that,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'even': 0.60; 'skip:u 10': 0.60; 'above,': 0.60; 'most': 0.60; 'skip:* 10': 0.61; 'such': 0.63; '(that': 0.65; 'here': 0.66; 'case?': 0.84; 'whereas': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Unicode Objects in Tuples Date: Fri, 11 Oct 2013 10:51:19 +0200 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Gmane-NNTP-Posting-Host: p5084bca8.dip0.t-ipconnect.de User-Agent: KNode/4.7.3 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: 62 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1381481463 news.xs4all.nl 15992 [2001:888:2000:d::a6]:50661 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:56666 Stephen Tucker wrote: > I am using IDLE, Python 2.7.2 on Windows 7, 64-bit. > > I have four questions: > > 1. Why is it that > print unicode_object > displays non-ASCII characters in the unicode object correctly, whereas > print (unicode_object, another_unicode_object) > displays non-ASCII characters in the unicode objects as escape sequences > (as repr() does)? > > 2. Given that this is actually *deliberately *the case (which I, at the > moment, am finding difficult to accept), what is the neatest (that is, the > most Pythonic) way to get non-ASCII characters in unicode objects in > tuples displayed correctly? "correct" being a synonym for "as I expect" ;) > 3. A similar thing happens when I write such objects and tuples to a file > opened by > codecs.open ( ..., "utf-8") > I have also found that, even though I use write to send the text to the > file, unicode objects not in tuples get their non-ASCII characters sent to > the file correctly, whereas, unicode objects in tuples get their > characters sent to the file as escape sequences. Why is this the case? > > 4. As for question 1 above, I ask here also: What is the neatest way to > get round this? I'll second Ben's recommendation of Python 3: >>> t = "mäßig", "müßig", "nötig" >>> print(t) ('mäßig', 'müßig', 'nötig') >>> print(*t) mäßig müßig nötig >>> print(*t, sep=", ") mäßig, müßig, nötig All three variants also work with files. Example: >>> with open("tmp.txt", "w") as outstream: ... print(*t, file=outstream) ... >>> open("tmp.txt").read() 'mäßig müßig nötig\n' Should you decide to stick with Python 2 you can use a helper function: >>> t = u"mäßig", u"müßig", u"nötig" >>> def pretty_tuple(t, sep=u", "): ... return sep.join(map(unicode, t)) ... >>> print pretty_tuple(t) mäßig, müßig, nötig >>> print pretty_tuple(t, sep=u" ") mäßig müßig nötig