Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #94428
| References | <mailman.864.1437567640.3674.python-list@python.org> <55afaad8$0$1646$c3e8da3$5496439d@news.astraweb.com> <87d1zjo1o4.fsf@handshake.de> |
|---|---|
| Date | 2015-07-23 16:13 +1000 |
| Subject | Re: Encoding of Python 2 string literals |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.901.1437631990.3674.python-list@python.org> (permalink) |
On Thu, Jul 23, 2015 at 3:58 PM, dieter <dieter@handshake.de> wrote:
> Steven D'Aprano <steve@pearwood.info> 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("текст")
>>>
>>> Is there any way for Entry() constructor to know that
>>> string "текст" passed into it is the utf-8 string?
>> ...
>> The right way to deal with this is to use an actual Unicode string:
>>
>> Entry(u"текст")
>>
>> 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
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
Re: Encoding of Python 2 string literals Steven D'Aprano <steve@pearwood.info> - 2015-07-23 00:38 +1000 Re: Encoding of Python 2 string literals Chris Angelico <rosuav@gmail.com> - 2015-07-23 00:54 +1000 Re: Encoding of Python 2 string literals dieter <dieter@handshake.de> - 2015-07-23 07:58 +0200 Re: Encoding of Python 2 string literals Chris Angelico <rosuav@gmail.com> - 2015-07-23 16:13 +1000
csiph-web