Path: csiph.com!news.mixmin.net!newsreader4.netcologne.de!news.netcologne.de!bcyclone05.am1.xlned.com!bcyclone05.am1.xlned.com!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!nzpost1.xs4all.net!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; 'subject:Python': 0.05; 'line:': 0.07; 'cc:addr:python-list': 0.09; 'scripts': 0.09; 'subject:characters': 0.09; 'subject:script': 0.09; 'python': 0.10; 'wed,': 0.15; 'encoding': 0.15; 'explicitly': 0.15; 'subject: \n ': 0.15; '"use': 0.16; "'a')": 0.16; "'a',": 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'omitting': 0.16; 'subject:non': 0.16; 'subject:run': 0.16; 'wrote:': 0.16; 'string': 0.17; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'aug': 0.20; 'not,': 0.22; 'am,': 0.23; 'header:In-Reply-To:1': 0.24; 'least': 0.27; 'message- id:@mail.gmail.com': 0.27; "i'd": 0.31; 'run': 0.33; 'avoiding': 0.33; 'instead,': 0.33; 'file': 0.34; 'received:google.com': 0.35; 'text': 0.35; 'but': 0.36; 'should': 0.36; 'subject:: ': 0.37; 'say': 0.37; 'means': 0.39; 'subject:-': 0.39; 'your': 0.60; 'default': 0.61; '26,': 0.72; 'chrisa': 0.84; 'to:none': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=1Rr0WIB12oZmjodAOe/KfXtu9SLTeO4gsYwllC+DzWw=; b=vUwee7NeOfufAaR02m+UJLHUsbu6O7LMQivA6ZHljWTi39+vJQKqnahiC6d3M0YfnU eS0vGV0mtRcskVCF4+pMajc23iZYPpQsnR2EpY0Vn055HUq0Q6Inf02nquT2mMDZPIlJ ZUJAA2l2wKAWB/jCXlw4ItyH91OwKeEKnCPXuYnyWIDyYkqQ4qeTj1RVTZaH+r6s79vy TaX4Chs0Etfio39QG2iOjrqYBGIN7LPfDkU1VhwgLYNhspabo/nxNtGmcNJn5nrD4GTl weUlF5KSNXWYA70gCLSEDjE6H8RxTkBBPd+o7ZupBatm9oUn9kPDjdapmJ+CE4RUMSHZ Z70A== MIME-Version: 1.0 X-Received: by 10.107.134.146 with SMTP id q18mr10994473ioi.31.1440544570600; Tue, 25 Aug 2015 16:16:10 -0700 (PDT) In-Reply-To: References: Date: Wed, 26 Aug 2015 09:16:10 +1000 Subject: Re: file.write() of non-ASCII characters differs in Interpreted Python than in script run From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ 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: 24 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1440544578 news.xs4all.nl 23735 [2001:888:2000:d::a6]:34056 X-Complaints-To: abuse@xs4all.nl X-Received-Bytes: 4072 X-Received-Body-CRC: 1164268702 Xref: csiph.com comp.lang.python:95642 On Wed, Aug 26, 2015 at 7:19 AM, RAH wrote: > rb = request_body.decode() # string I'd recommend avoiding this operation in Python 2. As of Python 3, omitting the encoding means "UTF-8", but in Python 2 it means "use the default encoding", and that often causes problems in scripts that run in various background environments. Instead, explicitly say what encoding you're using: rb = request_body.decode("UTF-8") Conversely, if you're using Python 3 for this, the default encoding is coming from this line: > h = open('logdict', 'a') Again, if you want this to be a text file with a specific encoding, say so: h = open('logdict', 'a', encoding='UTF-8') Give that a try and see if your problems disappear. If not, this should at least poke them with a pointy stick. ChrisA