is there a way to get the encoding of python file
Stef Mientki
stef.mientki at gmail.com
Mon Sep 13 15:00:19 EDT 2010
On 12-09-2010 19:28, Robert Kern wrote:
> On 9/12/10 4:14 AM, Stef Mientki wrote:
>> hello,
>>
>> Is it possible to get the encoding of a python file from the first source line,
>> (if there's any),
>> after importing it ( with '__import__' )
>>
>> # -*- coding: windows-1252 -*-
>
> The regular expression used to match the encoding declaration is given here:
>
> http://docs.python.org/reference/lexical_analysis.html#encoding-declarations
>
yes, but then I've to read the first line of the file myself.
In the meanwhile I found another (better ?) solution, (I'm using Python 2.6)
Place these 2 lines at the top of the file
# -*- coding: windows-1252 -*-
from __future__ import unicode_literals
or these
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
then you always get the correct unicode string back.
thanks,
Stef
More information about the Python-list
mailing list