Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder7.xlned.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!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; 'encoding': 0.05; 'output': 0.05; 'intermediate': 0.07; 'skip:" 60': 0.07; 'subject:file': 0.07; 'bytes.': 0.09; 'encode': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:How': 0.10; 'codec': 0.16; 'errors:': 0.16; 'operation,': 0.16; 'ordinal': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'unpack': 0.16; 'wrote:': 0.18; 'producing': 0.19; 'print': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'byte': 0.24; 'unicode': 0.24; 'shown': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'getting': 0.31; 'anyone': 0.31; 'file': 0.32; "can't": 0.35; 'advice': 0.35; 'but': 0.35; 'skip:f 40': 0.36; 'subject:data': 0.36; 'doing': 0.36; 'useful': 0.36; 'possible': 0.36; 'subject:?': 0.36; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'skip:u 10': 0.60; 'results.': 0.60; 'issues,': 0.61; 'received:173': 0.61; 'subject:this': 0.83; 'received:fios.verizon.net': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Jan Reedy Subject: Re: How do I encode and decode this data to write to a file? Date: Mon, 29 Apr 2013 18:02:08 -0400 References: <27s15a-943.ln1@chris.zbmc.eu> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130328 Thunderbird/17.0.5 In-Reply-To: <27s15a-943.ln1@chris.zbmc.eu> 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: 30 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1367272922 news.xs4all.nl 16005 [2001:888:2000:d::a6]:43680 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:44525 On 4/29/2013 5:47 AM, cl@isbd.net wrote: > case). Here's the traceback:- > > File "/usr/local/lib/python2.7/dist-packages/gallery/picture.py", line 361, > in createPictureHTML file.write("".join(html).encode('utf-8')) > UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 783: ordinal not in range(128) Generiric advice for anyone getting unicode errors: unpack the composition producing the error so that one can see which operation produced it. In this case s = "".join(html)\ s = s.encode('utf-8') file.write(s) This also makes it possible to print intermediate results. print(type(s), s) # would have been useful Doing so would have immediately shown that in this case the error was the encode operation, because s was already bytes. For many other posts, the error with the same type of message has been the print or write operation, do to output encoding issues, but that was not the case here.