Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.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; 'utf-8': 0.07; 'scripts': 0.09; 'encode': 0.09; 'mode,': 0.09; 'portable': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:script': 0.09; 'subject:not': 0.11; 'encoding': 0.15; '"a")': 0.16; '.txt': 0.16; 'codec': 0.16; 'encodings': 0.16; 'fine.': 0.16; 'instead:': 0.16; 'ordinal': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'subject:when': 0.16; 'wrote:': 0.17; 'exists': 0.17; 'specify': 0.17; 'changes': 0.20; 'skip:" 40': 0.20; 'import': 0.21; 'default,': 0.22; 'script': 0.24; 'tried': 0.25; 'header:User- Agent:1': 0.26; '(which': 0.26; 'disk': 0.27; 'header:X -Complaints-To:1': 0.28; 'run': 0.28; 'appending': 0.29; 'character': 0.29; 'writes': 0.30; 'error': 0.30; 'file': 0.32; 'to:addr:python-list': 0.33; "can't": 0.34; 'received:org': 0.36; 'but': 0.36; 'uses': 0.37; 'subject:: ': 0.38; 'mean': 0.38; 'sure': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'skip:u 10': 0.60; 'card': 0.62; 'subject:running': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: UnicodeEncodeError when not running script from IDE Date: Tue, 12 Feb 2013 15:49:42 +0100 Organization: None References: <650d144e-da3d-4ca7-ad3a-49f44ce9cbaa@googlegroups.com> <0d6d513d-fa12-4d51-a33d-7bb38f1ee6b2@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084b60a.dip.t-dialin.net 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: 31 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1360680572 news.xs4all.nl 6942 [2001:888:2000:d::a6]:39461 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:38751 Magnus Pettersson wrote: > I have tried now to take away printing to terminal and just keeping the > writing to a .txt file to disk (which is what the scripts purpose is): > > with open(filepath,"a") as f: > for card in cardlist: > f.write(card+"\n") > > The file it writes to exists and im just appending to it, but when i run > the script trough eclipse, all is fine. When i run in terminal i get this > error instead: > > File "K:\dev\python\webscraping\kanji_anki.py", line 69, in savefile > f.write(card+"\n") > UnicodeEncodeError: 'ascii' codec can't encode character u'\u898b' in > position 3 2: ordinal not in range(128) Are you sure you are writing the same data? That would mean that pydev changes the default encoding -- which is evil. A portable approach would be to use codecs.open() or io.open() instead of the built-in: import io with io.open(filepath, "a") as f: ... io.open() uses UTF-8 by default, but you can specify other encodings with io.open(filepath, mode, encoding=whatever).