Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed3.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'scripts': 0.09; '"\\n")': 0.09; 'encode': 0.09; 'subject:script': 0.09; 'subject:not': 0.11; 'encoding': 0.15; 'file,': 0.15; '.txt': 0.16; 'codec': 0.16; 'fine.': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'instead:': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'ordinal': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'subject:when': 0.16; 'wrote:': 0.17; 'exists': 0.17; 'skip:" 40': 0.20; 'script': 0.24; 'tried': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; '(which': 0.26; 'disk': 0.27; 'run': 0.28; 'appending': 0.29; 'character': 0.29; 'writes': 0.30; 'error': 0.30; 'file': 0.32; 'received:84': 0.32; 'to:addr:python-list': 0.33; "can't": 0.34; 'open': 0.35; 'but': 0.36; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'skip:u 10': 0.60; 'card': 0.62; 'header :Reply-To:1': 0.68; 'reply-to:no real name:2**0': 0.72; 'reply- to:addr:python.org': 0.84; 'subject:running': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.0 cv=HO4d4PRv c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=Tv4_tr9OjFAA:10 a=c9085cAK4sMA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=wvtB8Oauy_wA:10 a=B18WvQbJGzTtyoqPUcsA:9 a=wPNLvfGTeEIA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117 X-AUTH: mrabarnett:2500 Date: Tue, 12 Feb 2013 21:03:19 +0000 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130107 Thunderbird/17.0.2 MIME-Version: 1.0 To: python-list@python.org Subject: Re: UnicodeEncodeError when not running script from IDE References: <650d144e-da3d-4ca7-ad3a-49f44ce9cbaa@googlegroups.com> <0d6d513d-fa12-4d51-a33d-7bb38f1ee6b2@googlegroups.com> In-Reply-To: <0d6d513d-fa12-4d51-a33d-7bb38f1ee6b2@googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: python-list@python.org 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: 20 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1360702992 news.xs4all.nl 6904 [2001:888:2000:d::a6]:59705 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:38786 On 2013-02-12 14:24, 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) > When you open the file, tell it what encoding to use. For example: with open(filepath, "a", encoding="utf-8") as f: for card in cardlist: f.write(card + "\n")