Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!proxad.net!feeder1-2.proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!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; 'subject:not': 0.03; 'encoding': 0.05; 'interpreter': 0.05; 'output': 0.05; 'exists.': 0.07; 'needed,': 0.07; 'redirected': 0.07; "subject:' ": 0.07; 'utf-8': 0.07; 'bash': 0.09; 'encode': 0.09; 'subject:position': 0.09; 'sys,': 0.09; 'runs': 0.10; 'python': 0.11; 'assume': 0.14; '2.7': 0.14; '"u"': 0.16; 'ascii,': 0.16; 'codec': 0.16; 'codecs': 0.16; 'guessing': 0.16; 'hex': 0.16; 'ordinal': 0.16; 'side.': 0.16; 'sys.stdout': 0.16; 'wrote:': 0.18; 'do.': 0.18; 'trying': 0.19; 'thanks.': 0.20; 'import': 0.22; 'print': 0.22; 'header :User-Agent:1': 0.23; 'error': 0.23; 'byte': 0.24; 'bytes': 0.24; 'specify': 0.24; 'unicode': 0.24; '(or': 0.24; 'first,': 0.26; 'second': 0.26; 'asking': 0.27; 'header:In-Reply-To:1': 0.27; 'character': 0.29; "doesn't": 0.30; 'characters': 0.30; 'have,': 0.30; 'specified': 0.30; "i'm": 0.30; '(since': 0.31; 'equivalent.': 0.31; 'pipe': 0.31; 'file': 0.32; 'skip:c 30': 0.32; 'linux': 0.33; 'running': 0.33; '(most': 0.33; 'guess': 0.33; 'skip:# 10': 0.33; 'could': 0.34; 'problem': 0.35; "can't": 0.35; 'knows': 0.35; 'problem.': 0.35; 'case,': 0.35; 'but': 0.35; 'there': 0.35; 'version': 0.36; 'done': 0.36; "i'll": 0.36; 'hi,': 0.36; 'should': 0.36; 'depends': 0.38; 'window': 0.38; 'to:addr :python-list': 0.38; 'pm,': 0.38; 'track': 0.38; 'that,': 0.38; 'recent': 0.39; 'does': 0.39; 'skip:. 10': 0.39; 'subject:can': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'even': 0.60; 'skip:u 10': 0.60; 'tell': 0.60; 'conversion': 0.61; "you're": 0.61; "you've": 0.63; 'such': 0.63; 'refer': 0.63; 'different': 0.65; 'anything.': 0.68; 'received:74.208': 0.68; 'guessed': 0.84; 'received:74.208.4.194': 0.84; 'device,': 0.91; 'device.': 0.93 Date: Wed, 14 Jan 2015 05:24:52 -0500 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.4.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: UnicodeEncodeError: 'ascii' codec can't encode character u'\ua000' in position 0: ordinal not in range(128) References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Provags-ID: V02:K0:FRzI+Wlq/XpE2WW/DH3AUbGFqjlyf+YgcJ3aRKAH8Ln 0ATP6JGzSNSo7OjORpq2v40pG8qbFvltlP6svHKq3WAMxsJbYm II0WDhFbOtblVYk8JRPuha2ZVDsAWkreATHjdacCYR76d3ulpX sc976vqj0Y3OCKiBy/DyXuipY/Y247xzAKJrb6dDWUrdTRz+Gh 5psJ3vYhz5ptZ7jIN5tb8d5LrVNEfP6WyLrzfWjaL80+UMb62D 21u6Cxg2iCDRuw/RM3WXUkmSULUgotW7P0I7yGlN4+7ePq07y4 KcuDCUheCPx2m9ZuwCKTStnkM3iankIb++YfMnQkDU8HU0Q3fl GjRDX41KK+sqPz7DjYQo= X-UI-Out-Filterresults: notjunk:1; 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: 80 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1421231105 news.xs4all.nl 2834 [2001:888:2000:d::a6]:39099 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:83741 On 01/13/2015 10:26 PM, Peng Yu wrote: > Hi, > First, you should always specify your Python version and OS version when asking questions here. Even if you've been asking questions, many of us cannot keep track of everyone's specifics, and need to refer to a standard place, the head of the current thread. I'll assume you're using Python 2.7, on Linux or equivalent. > I am trying to understand what does encode() do. What are the hex > representations of "u" in main.py? Why there is UnicodeEncodeError > when main.py is piped to xxd? Why there is no such error when it is > not piped? Thanks. > > ~$ cat main.py > #!/usr/bin/env python > > u = unichr(40960) + u'abcd' + unichr(1972) > print u The unicode characters in 'u' must be decoded to a byte stream before sent to the standard out device. How they're decoded depends on the device, and what Python knows (or thinks it knows) about it. > ~$ cat main_encode.py > #!/usr/bin/env python > > u = unichr(40960) + u'abcd' + unichr(1972) > print u.encode('utf-8') Here, print is trying to send bytes to a byte-device, and doesn't try to second guess anything. > $ ./main.py > ꀀabcd޴ > ~$ cat main.sh > #!/usr/bin/env bash > > set -v > ./main.py | xxd > ./main_encode.py | xxd > > ~$ ./main.sh > ./main.py | xxd > Traceback (most recent call last): > File "./main.py", line 4, in > print u > UnicodeEncodeError: 'ascii' codec can't encode character u'\ua000' in > position 0: ordinal not in range(128) > ./main_encode.py | xxd > 0000000: ea80 8061 6263 64de b40a ...abcd... > I'm guessing (since i already guessed you're running on Linux) that in the main_encode case, you're printing to a terminal window that Python already knows is utf-8. But in the pipe case, it cannot tell what's on the other side. So it guesses ASCII, and runs into the conversion problem. (Everything's different in Python 3.x, though in general the problem still exists. If the interpreter cannot tell what encoding is needed, it has to guess.) There are ways to tell Python 2.7 what encoding a given file object should have, so you could tell Python to use utf-8 for sys.stdout. I don't know if that's the best answer, but here's what my notes say: import sys, codecs sys.stdout = codecs.getwriter('utf8')(sys.stdout) Once you've done that, print output will go through the specified codec on the way to the redirected pipe. -- DaveA