Path: csiph.com!optima2.xanadu-bbs.net!xanadu-bbs.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; 'string.': 0.04; 'subject:Python': 0.05; '-*-': 0.07; 'constructor': 0.07; 'utf-8': 0.07; 'cc:addr:python-list': 0.09; '22,': 0.09; 'coding:': 0.09; 'literal': 0.09; 'subject:string': 0.09; 'wed,': 0.15; 'encoding': 0.15; 'anatoly': 0.16; 'decode': 0.16; 'file?': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'wrote:': 0.16; 'string': 0.17; 'string,': 0.18; 'library': 0.20; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'so.': 0.22; 'parser': 0.22; 'defined': 0.23; 'import': 0.24; 'header:In- Reply-To:1': 0.24; "doesn't": 0.26; 'entered': 0.27; 'message- id:@mail.gmail.com': 0.27; 'declared': 0.29; 'entry': 0.31; 'source': 0.33; 'point,': 0.33; 'received:google.com': 0.35; 'unicode': 0.35; 'there': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'skip:e 20': 0.39; 'your': 0.60; 'matter': 0.63; '8bit%:50': 0.66; 'jul': 0.72; 'chrisa': 0.84; 'seen.': 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=jora1+INzyLLSDGsvua3x7Vc8jHIhfvSW6r8Gitlf1s=; b=VZJj5pkK/fOabn8WSbnnq2XfHf3CU+DwNFAlmPQHRQT9uwiR80FgvTU188h9afcBsw i+4Q1ViXnLjfwFaSQt1r6TWsqcZFEaTBh9OkGsDnX+CFuhzRiFgn3yT5GAhnG7yWjruz wELFDQg9WcrdINXeUe+6dDF4fv+xiZv24N3CIEo/7uK3K8L5drI0RPJQ/vhbsrSdMKbw wVtE1d5PyoBjCMlFBzEJNxIZ9pFJBqRi+VSeqxT4gB1qDP5WNG3Zkpso18jCNOx68VAG c9sm3k2+esr4B9b/FcNXTZMYKDU31p+h4AC5wX2TCz7KzoxS4sVkKevzTRj8vx+Sab8w OzZw== MIME-Version: 1.0 X-Received: by 10.107.4.1 with SMTP id 1mr3971428ioe.10.1437568796812; Wed, 22 Jul 2015 05:39:56 -0700 (PDT) In-Reply-To: References: Date: Wed, 22 Jul 2015 22:39:56 +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: 22 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1437568800 news.xs4all.nl 2878 [2001:888:2000:d::a6]:40395 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:94362 On Wed, Jul 22, 2015 at 8:17 PM, anatoly techtonik wr= ote: > 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 strin= g? I don't think so. However, if you declare that to be a Unicode string, the parser will decode it using the declared encoding, and it'll be a five-character string. At that point, it doesn't matter what your source encoding was, because the characters entered will match the characters seen. Entry(u"=D1=82=D0=B5=D0=BA=D1=81=D1=82") ChrisA