On 9/13/10 2:00 PM, Stef Mientki wrote:
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.
Ah. I see. You don't actually need to know the encoding; you just want to use
literals with raw, unescaped characters embedded in them.
This may interfere with the cases when you need a real str object. In Python
2.x, if you want a unicode literal, just use one like so: u'ß'. As long as the
encoding declaration is correct, this will work just fine.
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
--
http://mail.python.org/mailman/listinfo/python-list