Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #100257

Re: trying to force stdout to utf-8 with errors='ignore' or 'replace'

Path csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: trying to force stdout to utf-8 with errors='ignore' or 'replace'
Date Fri, 11 Dec 2015 12:37:01 +0100
Organization None
Lines 57
Message-ID <mailman.131.1449833844.12405.python-list@python.org> (permalink)
References <k6nqjcx307.ln2@news.ducksburg.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Trace news.uni-berlin.de cgQJ/u5seXAEevzimV3jcQB+Bz1JLnNsbDkT21FJv/BA==
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; 'else:': 0.03; 'modified': 0.05; 'none:': 0.05; 'extracted': 0.07; "subject:' ": 0.07; 'suppress': 0.07; 'true)': 0.07; 'utf-8': 0.07; 'encode': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'stdout': 0.09; 'subject:ignore': 0.09; 'subject:trying': 0.09; 'python': 0.10; 'output': 0.13; "(i'm": 0.16; 'adam': 0.16; 'codec': 0.16; 'decode': 0.16; 'instance)': 0.16; 'ordinal': 0.16; 'quoted': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'skip:q 30': 0.16; 'those,': 0.16; 'writer': 0.16; 'wrote:': 0.16; 'byte': 0.18; 'fix': 0.21; 'assuming': 0.22; 'fine,': 0.22; 'errors': 0.23; 'tried': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'skip:" 20': 0.26; 'skip:e 30': 0.27; "i'm": 0.30; 'error.': 0.31; 'skip:s 30': 0.31; "can't": 0.32; 'getting': 0.33; 'changing': 0.34; 'text': 0.35; 'feed': 0.35; 'unicode': 0.35; 'but': 0.36; 'beginning': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'skip:s 40': 0.38; 'stuff': 0.38; 'subject:-': 0.39; 'to:addr:python.org': 0.40; 'subject:with': 0.40; 'received:de': 0.40; 'body': 0.61; 'articles': 0.67; 'news': 0.68; 'article': 0.77; '(standard': 0.84
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host p57bd9023.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:100257

Show key headers only | View raw


Adam Funk wrote:

> I'm fiddling with a program that reads articles in the news spool
> using email.parser (standard library) &
> email_reply_parser.EmailReplyParser (installed with pip).  Reading is
> fine, & I don't get any errors writing output extracted from article
> bodies *until* I try to suppress invalid characters.  This works:
> 
>     if message.is_multipart():
>         body = message.get_payload(0, True)
>     else:
>         body = message.get_payload()
>     main_body = EmailReplyParser.parse_reply(body)
>     # fix quoted-printable stuff
>     if equals_regex.search(main_body):
>         main_body = quopri.decodestring(main_body)
>     # suppress attribution before quoted text
>     main_body = attrib_regex.sub('>', main_body)
>     # suppress sig
>     main_body = sig_regex.sub('\n', main_body)
>     main_body.strip()
>     stdout.write(main_body + '\n\n')
> 
> but the stdout includes invalid characters.  I tried adding this at
> the beginning
> 
>     if stdout.encoding is None:
>        writer = codecs.getwriter("utf-8")
>        stdout = writer(stdout, errors='replace')
> 
> and changing the output line to
> 
>     stdout.write(main_body.encode('utf-8', errors='replace') + '\n\n')
> 
> but with either or both of those, I get the dreaded
> "UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position
> 562: ordinal not in range(128)".  How can I force the output to be in
> UTF-8 & silently suppress invalid characters?

(I'm assuming you are using Python 2 and that main_body is a unicode 
instance)

Note that you are getting a *decode* error.

Either feed unicode to the modified stdout

>     writer = codecs.getwriter("utf-8")
>     stdout = writer(stdout, errors='replace')

      stdout.write(main_body + u'\n\n')

or encode manually and write to the original sys.stdout:

      sys.stdout.write(main_body.encode('utf-8', errors='replace') + '\n\n')


Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

trying to force stdout to utf-8 with errors='ignore' or 'replace' Adam Funk <a24061@ducksburg.com> - 2015-12-11 11:04 +0000
  Re: trying to force stdout to utf-8 with errors='ignore' or 'replace' Peter Otten <__peter__@web.de> - 2015-12-11 12:37 +0100
    Re: trying to force stdout to utf-8 with errors='ignore' or 'replace' Adam Funk <a24061@ducksburg.com> - 2015-12-11 16:54 +0000

csiph-web