Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!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; 'subject:Python': 0.05; 'compiler': 0.05; '-*-': 0.07; 'constructor': 0.07; 'utf-8': 0.07; 'cc:addr:python-list': 0.09; 'coding:': 0.09; 'encode': 0.09; 'literal': 0.09; 'says.': 0.09; 'subject:string': 0.09; 'python': 0.10; 'wed,': 0.15; 'encoding': 0.15; 'thu,': 0.15; '23,': 0.16; 'anatoly': 0.16; 'decoding': 0.16; 'file?': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'no;': 0.16; 'string:': 0.16; 'wrote:': 0.16; 'string': 0.17; '>>>': 0.20; 'library': 0.20; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'parsing': 0.22; 'defined': 0.23; 'needed.': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; '(which': 0.26; 'message-id:@mail.gmail.com': 0.27; 'actual': 0.28; 'cookie': 0.29; 'entry': 0.31; 'source': 0.33; "d'aprano": 0.33; 'steven': 0.33; 'file': 0.34; 'handle': 0.34; 'received:google.com': 0.35; 'saved': 0.35; 'unicode': 0.35; 'there': 0.36; 'lines': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'sure': 0.39; 'rather': 0.39; 'skip:e 20': 0.39; 'easy': 0.60; 'information': 0.63; 'you.': 0.64; '8bit%:50': 0.66; 'jul': 0.72; 'chrisa': 0.84; 'source:': 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:content-transfer-encoding; bh=Zbk+PBzxdgXeH6nQQpOugvCZfVOzwNty9HOqbTG4Iu0=; b=ZftwgwmRGVV5clIOJoMOl3QlSenn9TZ0ik7mHp9xfJTp72iVLgnqRW7+ujGUeZ+JqZ Qsp+aItONh9xFdLoyOtu37Zf/1MiUVDF/dpWho0TGHl69sygZmdZtpMvlctzKPxGSgeD ntsxBxFEDuiU56xvIkyTr/ujmgIbMBTZAQpepfWdXTuJyVdAihNUq48GJ7KIeYzo+KO3 F9owU8ve/CKLXWWLW42MWY5Zrvln6QlvqVAiusqHvJfyPEtntmfGUAjLdggSDS0nmw+1 x5yMYW+Foh6nlkwjaddbZxvhpPT1EdaWsxL34lraJYvMXRAEcvxu7SzeadgXXvJECjyg B8dA== MIME-Version: 1.0 X-Received: by 10.107.4.1 with SMTP id 1mr9826611ioe.10.1437631982713; Wed, 22 Jul 2015 23:13:02 -0700 (PDT) In-Reply-To: <87d1zjo1o4.fsf@handshake.de> References: <55afaad8$0$1646$c3e8da3$5496439d@news.astraweb.com> <87d1zjo1o4.fsf@handshake.de> Date: Thu, 23 Jul 2015 16:13:02 +1000 Subject: Re: Encoding of Python 2 string literals From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 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: 33 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1437631990 news.xs4all.nl 2961 [2001:888:2000:d::a6]:41284 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:94428 On Thu, Jul 23, 2015 at 3:58 PM, dieter wrote: > Steven D'Aprano writes: >> On Wed, 22 Jul 2015 08:17 pm, anatoly techtonik wrote: >>> Is there a way to know encoding of string (bytes) literal >>> defined in source file? For example, given that source: >>> >>> # -*- coding: utf-8 -*- >>> from library import Entry >>> Entry("=D1=82=D0=B5=D0=BA=D1=81=D1=82") >>> >>> Is there any way for Entry() constructor to know that >>> string "=D1=82=D0=B5=D0=BA=D1=81=D1=82" passed into it is the utf-8 str= ing? >> ... >> The right way to deal with this is to use an actual Unicode string: >> >> Entry(u"=D1=82=D0=B5=D0=BA=D1=81=D1=82") >> >> and make sure that the file is saved using UTF-8, as the encoding cookie >> says. > > In order to follow this recommendation, is there an easy way to > learn about the "encoding cookie"'s value -- rather than parsing > the first two lines of the source file (which may not always be available= ). No; you don't need to. If you use a Unicode string literal (as marked by the u"..." notation), the Python compiler will handle the decoding for you. The string that's passed to Entry() will simply be a string of Unicode codepoints - no encoding information needed. If you then want that in UTF-8, you can encode it explicitly. ChrisA