Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #18519
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!news-transit.tcx.org.uk!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| 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; 'sys': 0.05; 'none:': 0.07; 'subject:when': 0.07; 'python': 0.08; "(i'm": 0.09; '(it': 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; 'utf-8': 0.09; 'argument': 0.15; '-*-': 0.16; 'adam': 0.16; 'codec': 0.16; 'coding:': 0.16; 'encode': 0.16; 'example):': 0.16; 'ordinal': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'stdout': 0.16; 'subject:directly': 0.16; 'sys.stdout': 0.16; 'wrote:': 0.18; 'subject:not': 0.21; 'from:addr:web.de': 0.23; 'command': 0.24; 'fine': 0.24; "i'm": 0.26; 'import': 0.27; 'unicode': 0.29; 'print': 0.29; 'writer': 0.30; "can't": 0.32; 'rather': 0.33; 'header:X-Complaints-To:1': 0.33; 'to:addr:python- list': 0.34; 'character': 0.34; 'force': 0.34; 'running': 0.35; 'something': 0.35; '(for': 0.35; 'file': 0.36; 'but': 0.37; 'run': 0.37; 'using': 0.38; 'received:org': 0.38; 'correctly': 0.39; 'fail': 0.39; 'to:addr:python.org': 0.40; 'within': 0.60; 'subject:but': 0.67; 'funk': 0.84; 'subject:printing': 0.84; 'redirect': 0.91 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Peter Otten <__peter__@web.de> |
| Subject | Re: UnicodeEncodeError when piping stdout, but not when printing directly to the console |
| Date | Wed, 04 Jan 2012 22:49:52 +0100 |
| Organization | None |
| References | <l4set8xjf7.ln2@news.ducksburg.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="UTF-8" |
| Content-Transfer-Encoding | 8Bit |
| X-Gmane-NNTP-Posting-Host | p50848fdc.dip.t-dialin.net |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.12 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.4430.1325713810.27778.python-list@python.org> (permalink) |
| Lines | 34 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1325713810 news.xs4all.nl 6879 [2001:888:2000:d::a6]:33948 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:18519 |
Show key headers only | View raw
Adam Funk wrote:
> (I'm using Python 2.7.2+ on Ubuntu.)
>
> When I'm running my program in an xterm, the print command with an
> argument containing unicode works fine (it correctly detects my UTF-8
> environment). But when I run it with a pipe or redirect to a file (|
> or >), unicode strings fail with the following (for example):
>
> UnicodeEncodeError: 'ascii' codec can't encode character u'\u0107' in
> position 21: ordinal not in range(128)
>
> How can I force python (preferably within my python program, rather
> than having to set something externally) to treat stdout as UTF-8?
$ cat force_utf8.py
# -*- coding: utf-8 -*-
import sys
if sys.stdout.encoding is None:
import codecs
writer = codecs.getwriter("utf-8")
sys.stdout = writer(sys.stdout)
print u"Ähnlich üblich nötig"
$ python force_utf8.py
Ähnlich üblich nötig
$ python force_utf8.py | cat
Ähnlich üblich nötig
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
UnicodeEncodeError when piping stdout, but not when printing directly to the console Adam Funk <a24061@ducksburg.com> - 2012-01-04 21:15 +0000
Re: UnicodeEncodeError when piping stdout, but not when printing directly to the console Peter Otten <__peter__@web.de> - 2012-01-04 22:49 +0100
Re: UnicodeEncodeError when piping stdout, but not when printing directly to the console Adam Funk <a24061@ducksburg.com> - 2012-01-06 09:12 +0000
Re: UnicodeEncodeError when piping stdout, but not when printing directly to the console Peter Otten <__peter__@web.de> - 2012-01-06 10:34 +0100
Re: UnicodeEncodeError when piping stdout, but not when printing directly to the console Adam Funk <a24061@ducksburg.com> - 2012-01-06 14:22 +0000
csiph-web